2 回答

TA貢獻1735條經(jīng)驗 獲得超5個贊
除非您想做的不僅僅是設(shè)置幾個像素,否則使用Canvas具有一些更高級別繪圖基元(例如矩形和橢圓形)的小部件可能會更容易。
(這里有一些關(guān)于Canvas小部件的相當全面的 Tkinter 文檔。)
下面是經(jīng)過修改的代碼(加上其他一些代碼,通過遵循PEP 8 - Python 代碼指南的樣式指南并刪除一些我認為不必要和/或過于冗余的內(nèi)容,使其更具可讀性)。
它定義了一個新的輔助函數(shù),命名create_circle()為簡化對更通用的Canvas小部件create_oval()方法的調(diào)用。現(xiàn)在在您的saveCoordinates()函數(shù)中調(diào)用它(現(xiàn)在綁定到"<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條經(jīng)驗 獲得超1個贊
當他選擇第一個點時,該特定點會突出顯示(例如紅色或任何顏色);然后他選擇第二個點,第二個點也被突出顯示
我設(shè)法解決了所有步驟,除了第 3 步
該PhotoImage
班有用于設(shè)置像素的顏色的方法。例如,要將事件 x/y 處的像素設(shè)置為紅色,請執(zhí)行以下操作:
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
方法的第一個參數(shù)是一個顏色列表,它可以是已知的顏色名稱或 rgb 規(guī)范(例如:#ff0000
紅色等)。如果此處沒有足夠的數(shù)據(jù)來填充指定區(qū)域,則提供的數(shù)據(jù)將被平鋪。
該to
參數(shù)指定定義矩形區(qū)域的單個 x/y 坐標或兩個 x/y 坐標。
添加回答
舉報