Tkinter:为用户选择的点设置颜色

我有一张图片另存为image.png. 我的任务的工作流程是这样的:

  1. 在 Tkinter 中加载图像以及图像下方的“选择两点”按钮

  2. 用户在图像的两个点上用鼠标左键单击两次

  3. 当他选择第一个点时,该特定点会突出显示(例如红色或任何颜色);然后他选择第二个点,第二个点也被突出显示

  4. 两点的(x,y)坐标存储在全局变量中,以后会用到

  5. 一旦用户选择了两个点,第二个“完成!” 按钮出现。单击此按钮时,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"        

慕桂英3389331
浏览 141回答 2
2回答

喵喔喔

除非您想做的不仅仅是设置几个像素,否则使用Canvas具有一些更高级别绘图基元(例如矩形和椭圆形)的小部件可能会更容易。(这里有一些关于Canvas小部件的相当全面的 Tkinter 文档。)下面是经过修改的代码(加上其他一些代码,通过遵循PEP 8 - Python 代码指南的样式指南并删除一些我认为不必要和/或过于冗余的内容,使其更具可读性)。它定义了一个新的辅助函数,命名create_circle()为简化对更通用的Canvas小部件create_oval()方法的调用。现在在您的saveCoordinates()函数中调用它(现在绑定到"<Button 1>"新Canvas对象的事件而不是Label您正在使用的事件)。from tkinter import *root = Tk()&nbsp; # create a windowframe = Frame(root)&nbsp; # define upper framemiddleframe = Frame(root)&nbsp; # define middle frameexitFrame = Frame(root)&nbsp; # define exit frameframe.pack()&nbsp; # pack the framemiddleframe.pack()&nbsp; # pack the subframeexitFrame.pack(side='bottom')&nbsp; # pack the exit frame# function that closes the GUIdef close_window():&nbsp; &nbsp; root.destroy()img = PhotoImage(file="myimage.png")&nbsp; # load the imagecanvas = Canvas(frame, width=img.width(), height=img.height(), borderwidth=0)canvas.grid(row=0, column=0)canvas.create_image(0, 0, image=img, anchor=NW)# make the user select some pointsx_Coordinates = []&nbsp; # list for storing x-axis coordinatesy_Coordinates = []&nbsp; # list for storing y-axis coordinatesclicks = 0def create_circle(canvas, x, y, radius, **kwargs):&nbsp; &nbsp; return canvas.create_oval(x-radius, y-radius, x+radius, y+radius, **kwargs)def countClicks():&nbsp; &nbsp; global clicks&nbsp; &nbsp; clicks += 1&nbsp; &nbsp; # if the user has selected 2 points, add a button that closes the window&nbsp; &nbsp; if clicks == 2:&nbsp; &nbsp; &nbsp; &nbsp; # link the closing function to the button&nbsp; &nbsp; &nbsp; &nbsp; exit_button = Button(exitFrame, state="normal", text="Done!",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;command=close_window)&nbsp; &nbsp; &nbsp; &nbsp; exit_button.grid(row=2, column=0, pady=5)&nbsp; # set button position with "grid"def selectPoints():&nbsp; # function called when user clicks the button "select two points"&nbsp; &nbsp; # link the function to the left-mouse-click event&nbsp; &nbsp; canvas.bind("<Button 1>", saveCoordinates)&nbsp; &nbsp; # link closing function to the button&nbsp; &nbsp; exit_button = Button (exitFrame, state="disabled", text="Done!",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command=close_window)&nbsp; &nbsp; exit_button.grid(row=2, column=0, pady=5)&nbsp; # set button position with "grid"&nbsp; &nbsp; button_select_points.config(state="disabled") # switch button state to "disabled"def saveCoordinates(event): # function called when left-mouse-button is clicked&nbsp; &nbsp; x_coordinate = event.x&nbsp; # save x and y coordinates selected by the user&nbsp; &nbsp; y_coordinate = event.y&nbsp; &nbsp; x_Coordinates.append(x_coordinate)&nbsp; &nbsp; y_Coordinates.append(y_coordinate)&nbsp; &nbsp; # Display a small dot showing position of point.&nbsp; &nbsp; create_circle(canvas, x_coordinate, y_coordinate, radius=3, fill='red')&nbsp; &nbsp; countClicks()# insert button and link it to "selectPoints"button_select_points = Button(middleframe, text="select two points",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command=selectPoints)button_select_points.grid(row=1, column=0, pady=5)root.mainloop()&nbsp; # keep the GUI open

慕神8447489

当他选择第一个点时,该特定点会突出显示(例如红色或任何颜色);然后他选择第二个点,第二个点也被突出显示我设法解决了所有步骤,除了第 3 步该PhotoImage班有用于设置像素的颜色的方法。例如,要将事件 x/y 处的像素设置为红色,请执行以下操作:img.put(("red",),&nbsp;to=(event.x,&nbsp;event.y))由于单个像素确实很难看到,因此您可以很容易地绘制一个以该点为中心的 3x3 小矩形。下面的示例将红色置于正方形中的像素 from&nbsp;event.x-1, event.y-1to&nbsp;event.x+1, event.y+1:img.put(("red",),&nbsp;to=(event.x-1,&nbsp;event.y-1,&nbsp;event.x+1,&nbsp;event.y+1))该put方法的第一个参数是一个颜色列表,它可以是已知的颜色名称或 rgb 规范(例如:#ff0000红色等)。如果此处没有足够的数据来填充指定区域,则提供的数据将被平铺。该to参数指定定义矩形区域的单个 x/y 坐标或两个 x/y 坐标。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python