2 回答
TA贡献1735条经验 获得超5个赞
除非您想做的不仅仅是设置几个像素,否则使用Canvas具有一些更高级别绘图基元(例如矩形和椭圆形)的小部件可能会更容易。
(这里有一些关于Canvas小部件的相当全面的 Tkinter 文档。)
下面是经过修改的代码(加上其他一些代码,通过遵循PEP 8 - Python 代码指南的样式指南并删除一些我认为不必要和/或过于冗余的内容,使其更具可读性)。
它定义了一个新的辅助函数,命名create_circle()为简化对更通用的Canvas小部件create_oval()方法的调用。现在在您的saveCoordinates()函数中调用它(现在绑定到"<Button 1>"新Canvas对象的事件而不是Label您正在使用的事件)。
from tkinter import *
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()
img = PhotoImage(file="myimage.png") # load the image
canvas = 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 points
x_Coordinates = [] # list for storing x-axis coordinates
y_Coordinates = [] # list for storing y-axis coordinates
clicks = 0
def create_circle(canvas, x, y, radius, **kwargs):
return canvas.create_oval(x-radius, y-radius, x+radius, y+radius, **kwargs)
def countClicks():
global clicks
clicks += 1
# if the user has selected 2 points, add a button that closes the window
if clicks == 2:
# link the closing function to the button
exit_button = Button(exitFrame, state="normal", text="Done!",
command=close_window)
exit_button.grid(row=2, column=0, pady=5) # set button position with "grid"
def selectPoints(): # function called when user clicks the button "select two points"
# link the function to the left-mouse-click event
canvas.bind("<Button 1>", saveCoordinates)
# link closing function to the button
exit_button = Button (exitFrame, state="disabled", text="Done!",
command=close_window)
exit_button.grid(row=2, column=0, pady=5) # set button position with "grid"
button_select_points.config(state="disabled") # switch button state to "disabled"
def saveCoordinates(event): # function called when left-mouse-button is clicked
x_coordinate = event.x # save x and y coordinates selected by the user
y_coordinate = event.y
x_Coordinates.append(x_coordinate)
y_Coordinates.append(y_coordinate)
# Display a small dot showing position of point.
create_circle(canvas, x_coordinate, y_coordinate, radius=3, fill='red')
countClicks()
# insert button and link it to "selectPoints"
button_select_points = Button(middleframe, text="select two points",
command=selectPoints)
button_select_points.grid(row=1, column=0, pady=5)
root.mainloop() # keep the GUI open
TA贡献1780条经验 获得超1个赞
当他选择第一个点时,该特定点会突出显示(例如红色或任何颜色);然后他选择第二个点,第二个点也被突出显示
我设法解决了所有步骤,除了第 3 步
该PhotoImage
班有用于设置像素的颜色的方法。例如,要将事件 x/y 处的像素设置为红色,请执行以下操作:
img.put(("red",), to=(event.x, event.y))
由于单个像素确实很难看到,因此您可以很容易地绘制一个以该点为中心的 3x3 小矩形。下面的示例将红色置于正方形中的像素 from event.x-1, event.y-1
to event.x+1, event.y+1
:
img.put(("red",), to=(event.x-1, event.y-1, event.x+1, event.y+1))
该put
方法的第一个参数是一个颜色列表,它可以是已知的颜色名称或 rgb 规范(例如:#ff0000
红色等)。如果此处没有足够的数据来填充指定区域,则提供的数据将被平铺。
该to
参数指定定义矩形区域的单个 x/y 坐标或两个 x/y 坐标。
添加回答
举报