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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

python tkinter 如何更改特定網(wǎng)格空間上的小部件標(biāo)簽?

python tkinter 如何更改特定網(wǎng)格空間上的小部件標(biāo)簽?

慕俠2389804 2023-01-04 16:36:19
我有一個(gè) 3x3 的按鈕網(wǎng)格,所有這些按鈕都綁定到相同的事件函數(shù)。此函數(shù)根據(jù)單擊的按鈕更改特定按鈕的標(biāo)簽。我希望能夠通過查看按鈕在網(wǎng)格上的位置(即行值和列值)來選擇將影響哪些按鈕。例如,我希望能夠說“考慮到點(diǎn)擊的按鈕是(第 1 行,第 2 列),我想更改(第 2 行,第 0 列)和(第 1 行,第 1 列)按鈕的標(biāo)簽.我知道如何找到被點(diǎn)擊按鈕的行和列:import tkinterdef click(event):    space = event.widget    space_label = space['text']    row = space.grid_info()['row'] #get the row of clicked button    column = space.grid_info()['column'] #get the column of clicked buttonboard = tkinter.Tk()for i in range(0,9): #creates the 3x3 grid of buttons    button = tkinter.Button(text = "0")    if i in range(0,3):        r = 0    elif i in range(3,6):        r = 1    else:        r = 2    button.grid(row = r, column = i%3)    button.bind("<Button-1>",click)board.mainloop()但是我無法弄清楚如何只給定網(wǎng)格的一行和一列來訪問按鈕的標(biāo)簽。PS 編碼新手,抱歉,如果這很明顯,我環(huán)顧四周但找不到任何足夠相似的問題。
查看完整描述

1 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果您打算在創(chuàng)建一個(gè)小部件并將其添加到您的界面后對(duì)其進(jìn)行任何操作,最好保留一個(gè)參考。


這是對(duì)您的代碼的修改,它創(chuàng)建了一個(gè)字典button_dict來存儲(chǔ)您的每個(gè)按鈕。鍵是元組(row,column)。我將button_dict作為附加輸入添加到您的click函數(shù)中,并修改了按鈕綁定以使用 包含這個(gè)新輸入lambda。


import tkinter


def click(event,button_dict):

    space = event.widget

    space_label = space['text']

    row = space.grid_info()['row'] #get the row of clicked button

    column = space.grid_info()['column'] #get the column of clicked button


    button = button_dict[(row,column)] # Retrieve our button using the row/col

    print(button['text']) # Print button text for demonstration


board = tkinter.Tk()


button_dict = {} # Store your button references here


for i in range(0,9): #creates the 3x3 grid of buttons


    button = tkinter.Button(text = i) # Changed the text for demonstration


    if i in range(0,3):

        r = 0

    elif i in range(3,6):

        r = 1

    else:

        r = 2


    button.grid(row = r, column = i%3)

    button.bind("<Button-1>",lambda event: click(event,button_dict))


    button_dict[(r,i%3)] = button # Add reference to your button dictionary


board.mainloop()

如果您只對(duì)按下的按鈕感興趣,您可以簡(jiǎn)單地訪問該event.widget屬性。從你的例子來看,聽起來你想修改每個(gè)事件的任意數(shù)量的按鈕,所以我認(rèn)為字典會(huì)給你更多的通用性。


查看完整回答
反對(duì) 回復(fù) 2023-01-04
  • 1 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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