亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

再次單擊時如何將按鈕顏色更改回原始顏色

再次單擊時如何將按鈕顏色更改回原始顏色

至尊寶的傳說 2022-01-05 10:50:13
我試圖在單擊時將按鈕顏色更改為黑色,然后在再次單擊時將其改回白色。我正在嘗試為學校項目制作 Game Of Life。我試過 if 語句,但它不會變回白色,也許我錯過了一些簡單的東西。這是代碼:from tkinter import *class GUI(Frame):   def __init__(self, master=None):        Frame.__init__(self, master)        master.title("Window") #Window title        self.pack()        master.geometry("1280x720") #Window size        self.button={}#Dictionary for buttons        self.create_button()    def create_button(self):        indexList =[i for i in range(1000)]        self._button = Button(self, bg='white')        print(self._button.cget('bg'))        xPos = 0        yPos = 0        for index in indexList:            if(yPos == 40):                xPos = xPos + 20                yPos = 0            if(xPos == 10):                yPos = 8            self._button = Button(self, height=2, width=4, command = lambda             i = index: self.changecolour(i))            self.button[index] = self._button            self._button.grid(row=xPos, column =yPos)            yPos = yPos + 1    def changecolour(self,index):        aList = []        for i in range(1000):            aList.append([i,0])        for i in aList:            if index == i[0]:                if 0 == i[1]:                     self.button[index].configure(bg = 'black')                    i[1] = 1                else:                    self.button[index].configure(bg = 'white')                    i[1] = 0root = Tk()game_gui = GUI(master=root)game_gui.mainloop()如您所見,它將按鈕顏色更改為黑色,再次單擊時應將其更改回白色,但它似乎只是忽略了 if 語句。
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

我認為這是問題所在:


aList 不是全局列表


aListchangecolour()每次運行子程序時都作為本地列表創建


這意味著當你這樣做i[1] = 1或i[1] = 0它只改變本地列表時 aList。當子程序再次運行時,aList會創建一個新的作為新的本地列表。


解決aList主程序中定義的問題并使其成為全局列表:


from tkinter import *


class GUI(Frame):

   def __init__(self, master=None):


        Frame.__init__(self, master)

        master.title("Window") #Window title

        self.pack()


        master.geometry("1280x720") #Window size


        self.button={}#Dictionary for buttons


        self.create_button()


    def create_button(self):

        indexList =[i for i in range(1000)]

        self._button = Button(self, bg='white')

        print(self._button.cget('bg'))


        xPos = 0

        yPos = 0

        for index in indexList:

            if(yPos == 40):

                xPos = xPos + 20

                yPos = 0

            if(xPos == 10):

                yPos = 8


            self._button = Button(self, height=2, width=4, command = lambda 

            i = index: self.changecolour(i))

            self.button[index] = self._button

            self._button.grid(row=xPos, column =yPos)

            yPos = yPos + 1



    def changecolour(self,index):

        #aList IS NO LONGER CREATED HERE


        for i in range(1000):

            aList.append([i,0])


        for i in aList:

            if index == i[0]:

                if 0 == i[1]: 

                    self.button[index].configure(bg = 'black')

                    i[1] = 1

                else:

                    self.button[index].configure(bg = 'white')

                    i[1] = 0


global aList #MAKE IT A GLOBAL LIST

aList = [] #CREATE THE EMPTY aList LIST

root = Tk() 

game_gui = GUI(master=root)

game_gui.mainloop()


查看完整回答
反對 回復 2022-01-05
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號