簡體   English   中英

Python Tkinter-函數創建的對象的名稱

[英]Python Tkinter - Name of objects created by function

我通過此功能創建了一組按鈕:

from tkinter import *
from random import randint

window = Tk()
window.title("Test")
window.geometry('200x200')
color = ["red","blue","green","yellow","black","purple","orange"]
RandInt = 0
j = 0
h = 0

def ButtonDef(xvar = 0,yvar = 0): 
    btn = Button(command =lambda:[RandomColor()])
    btn.grid()
    btn.place(x = xvar*50, y = yvar*50, width = 50, height = 50)

    def RandomColor():
        RandInt = randint (0,6)
        btn.configure(bg = color[RandInt])

while j in range (4):
    i = 0
    j += 1
    while i in range (4):
        ButtonDef(i,h)
        i += 1
        if i == 4:
            h += 1
window.mainloop()

但是,我的RandomColor()函數僅更改我按下的按鈕的顏色-這也很有趣,但是我不知道如何使它隨機更改所有按鈕的顏色。 當通過函數創建時,我會猜到創建的所有按鈕都被命名為“ btn”,因為那是我給它們的唯一名稱。

我如何解決一組按功能創建的按鈕中的所有(或一個特定)按鈕? 或簡單地說,所有這些按鈕的名稱是什么? 他們共享名稱“ btn”嗎? 他們是否分配了隱藏的ID?

讓我們嘗試

btn = []
for i in range(16):
    btn.append(Button(window))

它將創建一個按鈕數組。 因此,您可以通過btn [i] .configure(command = lambda:[RandomColor()])或其他方式進行訪問。

您的問題背后的原因:

問題是執行此行時:循環結束時, btn = Button(command =lambda:[RandomColor()]) ,您只能獲得對最后創建的按鈕的引用。 您沒有參考其他按鈕。

解:

如果您依賴winfo_children()則可以克服此問題。

您必須執行2個步驟來解決您的問題:

首先,將btn = Button(command =lambda:[RandomColor()])更改為btn = Button(window, command=lambda:[RandomColor()]) 這只是意味着我們將每個創建的按鈕附加到稱為window的父窗口小部件。

然后,您需要更改的只是RandomColor()函數體,如下所示:

def RandomColor():       
    for child in window.winfo_children():
        child.configure(bg=color[randint(0,6)])

演示:

在此處輸入圖片說明

這樣可以解決您的問題,但是您的代碼不干凈。 您可以在Code Review網站上尋求改進代碼的建議。

編輯:

這是您在注釋中描述的方案的解決方案。

請注意,我必須從頭開始編寫干凈的代碼(我知道您是今天才開始的,所以我不會怪您)。 在這段代碼中,我保留了我創建的每個按鈕的參考:

import tkinter as tk
import random


class ChangeBottomRightColor(tk.Frame):

   def __init__(self, master):
       self.master = master
       tk.Frame.__init__(self, self.master)
       self.__colors = ["red","blue","green","yellow","black","purple","orange"]
       self.configure_gui()
       self.create_widgets()

   def configure_gui(self):
       pass

   def create_widgets(self):
       self.create_buttons()

   def create_buttons(self):
       self.buttons = {}
       c = 0
       for i in range(4):
           for j in range(4):              
               self.buttons['button{}'.format(c)] = tk.Button(self.master)
               self.buttons['button{}'.format(c)].grid(row=i, column=j)
               self.buttons['button{}'.format(c)].config(width=3, height=3)
               self.buttons['button{}'.format(c)].config(command=self.change_bottom_right_button_color)
               c += 1

   def get_random_color(self):
       return random.choice(self.__colors)

   def change_bottom_right_button_color(self):       
       self.buttons['button{}'.format(15)].config(bg=self.get_random_color())       



if __name__ == '__main__':
   root = tk.Tk()
   main_app =  ChangeBottomRightColor(root)
   root.mainloop()

演示:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM