我有一张图片另存为image.png
. 我的任务的工作流程是这样的:
在 Tkinter 中加载图像以及图像下方的“选择两点”按钮
用户在图像的两个点上用鼠标左键单击两次
当他选择第一个点时,该特定点会突出显示(例如红色或任何颜色);然后他选择第二个点,第二个点也被突出显示
两点的(x,y)坐标存储在全局变量中,以后会用到
一旦用户选择了两个点,第二个“完成!” 按钮出现。单击此按钮时,GUI 关闭。注意,我希望这两个点保持突出显示,直到用户单击关闭按钮,以便他/她知道他/她单击的位置
我设法解决了所有步骤,除了step 3。我发现的最相似的事情是创建一个带有 的矩形canvas.create_rectangle(x,y,x+1,y+1,fill="red")
,但首先我更喜欢一个圆形,其次我无法将 链接canvas
到我的Label
任何帮助将不胜感激:D
到目前为止,这是我的代码:
root = Tk() # create a window
frame = Frame(root) # define upper frame
middleframe = Frame(root) # define middle frame
exitFrame = Frame(root) # define exit frame
frame.pack() # pack the frame
middleframe.pack() # pack the subframe
exitFrame.pack(side = 'bottom') # pack the exit frame
# function that closes the GUI
def close_window():
root.destroy()
# load the image
img = PhotoImage(file="image.png") # save the image
panel = Label(frame, image=img) # display the image as a label
panel.grid(row=0, column=0) # pack the image
# make the user select some points
global x_Coordinates # initialize empty list for storing x-axis coordinates
global y_Coordinates # initialize empty list for storing y-axis coordinates
x_Coordinates = []
y_Coordinates = []
clicks = 0
def countClicks():
global clicks # this will use the variable to count
clicks = clicks + 1 # increment "clicks"
if clicks == 2: # if the user has selected 2 points, add a button that closes the window
exit_button = Button(exitFrame, state = "normal", text = "Done!", command = close_window) # link the closing function to the button
exit_button.grid(row=2, column=0, pady=5) # set button position with "grid"
喵喔喔
慕神8447489
相关分类