第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Tkinter:為用戶選擇的點設(shè)置顏色

Tkinter:為用戶選擇的點設(shè)置顏色

慕桂英3389331 2021-08-11 19:43:18
我有一張圖片另存為image.png. 我的任務(wù)的工作流程是這樣的:在 Tkinter 中加載圖像以及圖像下方的“選擇兩點”按鈕用戶在圖像的兩個點上用鼠標左鍵單擊兩次當他選擇第一個點時,該特定點會突出顯示(例如紅色或任何顏色);然后他選擇第二個點,第二個點也被突出顯示兩點的(x,y)坐標存儲在全局變量中,以后會用到一旦用戶選擇了兩個點,第二個“完成!” 按鈕出現(xiàn)。單擊此按鈕時,GUI 關(guān)閉。注意,我希望這兩個點保持突出顯示,直到用戶單擊關(guān)閉按鈕,以便他/她知道他/她單擊的位置我設(shè)法解決了所有步驟,除了step 3。我發(fā)現(xiàn)的最相似的事情是創(chuàng)建一個帶有 的矩形canvas.create_rectangle(x,y,x+1,y+1,fill="red"),但首先我更喜歡一個圓形,其次我無法將 鏈接canvas到我的Label任何幫助將不勝感激:D到目前為止,這是我的代碼:root = Tk()  # create a windowframe = Frame(root)  # define upper framemiddleframe = Frame(root)  # define middle frameexitFrame = Frame(root)  # define exit frameframe.pack()  # pack the framemiddleframe.pack()  # pack the subframeexitFrame.pack(side = 'bottom')  # pack the exit frame# function that closes the GUIdef close_window():     root.destroy()# load the imageimg = PhotoImage(file="image.png")  # save the imagepanel = Label(frame, image=img)  # display the image as a labelpanel.grid(row=0, column=0)  # pack the image# make the user select some pointsglobal x_Coordinates  # initialize empty list for storing x-axis coordinatesglobal y_Coordinates  # initialize empty list for storing y-axis coordinatesx_Coordinates = []y_Coordinates = []clicks = 0def 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"        
查看完整描述

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


查看完整回答
反對 回復(fù) 2021-08-11
?
慕神8447489

TA貢獻1780條經(jīng)驗 獲得超1個贊

  1. 當他選擇第一個點時,該特定點會突出顯示(例如紅色或任何顏色);然后他選擇第二個點,第二個點也被突出顯示

我設(shè)法解決了所有步驟,除了第 3 步

PhotoImage班有用于設(shè)置像素的顏色的方法。例如,要將事件 x/y 處的像素設(shè)置為紅色,請執(zhí)行以下操作:

img.put(("red",), to=(event.x, event.y))

由于單個像素確實很難看到,因此您可以很容易地繪制一個以該點為中心的 3x3 小矩形。下面的示例將紅色置于正方形中的像素 from event.x-1, event.y-1to 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 坐標。


查看完整回答
反對 回復(fù) 2021-08-11
  • 2 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號