我的tkinter应用程序需要一些帮助。 创建该应用程序的主要目的是在按下按钮时获取按钮名称,将其存储在变量中,然后在按下确认键时,获取包含所有这些“字符串”变量的字典。 “获取按钮名称”可以正常工作,但是当界面中的按钮过多时,我会遇到问题。 此代码集出现问题:
我尝试清除self.list_buttons
内容,但没有成功。
当self.list_buttons
的值小于10时,无论是在modulo_matriz_conjunto
还是modulo_matriz_elemento
内部,当我有10个以上的值时,其他按钮(在此范围之外)代码的一部分)停止工作。所有按钮的创建都是通过这种类型的for
循环完成的,其中self.list_buttons
将文本写入按钮内部,而self.buttons_conj
则是为了存储在for循环中创建的按钮。我想知道这种语法是否需要我的计算机太多的内存,并且因此不能正常工作。谁能帮我解决这个问题?
def modulo_matriz_conjunto(self):
self.font_main = ("Calibri", 9)
self.list_buttons = ["button1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
self.buttons_conj = []
button_index = 0
for name in self.list_buttons:
if button_index < 5:
self.conj_button = tk.Button(self.frame_matrix_line1)
elif 5 <= button_index < 10:
self.conj_button = tk.Button(self.frame_matrix_line2)
elif 10 <= button_index < 15:
self.conj_button = tk.Button(self.frame_matrix_line3)
elif 15 <= button_index < 20:
self.conj_button = tk.Button(self.frame_matrix_line4)
else:
self.conj_button = tk.Button(self.frame_matrix_line5)
self.conj_button["text"] = name
self.conj_button["height"] = 2
self.conj_button["width"] = 15
self.conj_button["bg"] = color_blue
self.conj_button["fg"] = "white"
self.conj_button["activebackground"] = color_dgreen
self.conj_button["activeforeground"] = "white"
self.conj_button["font"] = self.font_main
self.conj_button["command"] = partial(self.func_botoes_conjunto, name)
self.conj_button["state"] = "normal" # normal, disabled or active
self.conj_button.pack(side="left", expand=True)
self.buttons_conj.append(self.conj_button)
button_index += 1
def modulo_matriz_elemento(self):
self.list_buttons = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
self.buttons_elem = []
button_index = 0
tk.Label(self.frame_matrix_line6, height="2").pack(side="top", fill="both", expand=True)
for name in self.list_buttons:
if button_index < 5:
self.elem_button = tk.Button(self.frame_matrix_line6)
elif 5 <= button_index < 10:
self.elem_button = tk.Button(self.frame_matrix_line7)
elif 10 <= button_index < 15:
self.elem_button = tk.Button(self.frame_matrix_line8)
elif 15 <= button_index < 20:
self.elem_button = tk.Button(self.frame_matrix_line9)
else:
self.elem_button = tk.Button(self.frame_matrix_line10)
self.elem_button["text"] = name
self.elem_button["height"] = 2
self.elem_button["width"] = 15
self.elem_button["bg"] = color_blue
self.elem_button["fg"] = "white"
self.elem_button["activebackground"] = color_dgreen
self.elem_button["activeforeground"] = "white"
self.elem_button["font"] = self.font_main
self.elem_button["command"] = partial(self.func_botoes_elemento, name)
self.elem_button["state"] = "normal" # normal, disabled or active
self.elem_button.pack(side="left", expand=True)
self.buttons_elem.append(self.elem_button)
button_index += 1
def func_botoes_conjunto(self, text_inside_button):
for button in self.buttons_conj:
button["relief"] = "raised"
button["bg"] = color_blue
for button in self.buttons_conj:
if button["text"] == text_inside_button:
button["bg"] = color_dgreen
button["relief"] = "sunken"
print(text_inside_button)
self.final_information["Conjunto"] = text_inside_button
self.modulo_matriz_elemento()
def func_botoes_elemento(self, text_inside_button):
for button in self.buttons_elem:
button["relief"] = "raised"
button["bg"] = color_blue
for button in self.buttons_elem:
if button["text"] == text_inside_button:
button["bg"] = color_dgreen
button["relief"] = "sunken"
print(text_inside_button)
self.final_information["Elemento"] = text_inside_button
这是完整的代码: EDIT2:我忘记了MainFrames之前的类,这是下面的代码
class MainFrames: # frame top, left and main3
def __init__(self, master=None):
# main1 top frame
self.font_main = ("Calibri", 18)
self.frame_top = ttk.Frame(master)
self.frame_top.pack(side="top", fill="x")
self.photo = PhotoImage(file="logo_myself.png")
self.logo = tk.Label(self.frame_top, image=self.photo)
self.logo["bg"] = color_blue
self.photo.photo = self.photo
self.logo.pack(side="left")
self.top_name = tk.Label(self.frame_top)
self.top_name["text"] = "Digital"
self.top_name["bg"] = color_blue
self.top_name["fg"] = "white"
self.top_name["font"] = self.font_main
self.top_name["anchor"] = "w"
self.top_name.pack(side="left", fill="both")
self.top_name = tk.Label(self.frame_top)
self.top_name["text"] = "Hora a Hora FS"
self.top_name["bg"] = color_blue
self.top_name["fg"] = "white"
self.top_name["font"] = self.font_main
self.top_name["anchor"] = "w"
self.top_name.pack(side="left", fill="both", expand=True)
self.top_name = tk.Label(self.frame_top)
self.top_name["text"] = "9999"
self.top_name["bg"] = color_blue
self.top_name["fg"] = "white"
self.top_name["font"] = self.font_main
self.top_name["anchor"] = "w"
self.top_name.pack(side="left", fill="both", expand=True)
self.top_name = tk.Label(self.frame_top)
self.top_name["text"] = self.get_datatime(0) #for test only not working
self.top_name["bg"] = color_blue
self.top_name["fg"] = "white"
self.top_name["font"] = self.font_main
self.top_name["anchor"] = "w"
self.top_name.pack(side="left", fill="both", expand=True)
self.top_name = tk.Label(self.frame_top)
self.top_name["text"] = self.user_attribution(2)
self.top_name["bg"] = color_blue
self.top_name["fg"] = "white"
self.top_name["font"] = self.font_main
self.top_name["anchor"] = "e"
self.top_name.pack(side="left", fill="both", expand=True)
# main2 left frame
self.font_main = ("Calibri", 14)
self.frame_left = ttk.Frame(master)
self.frame_left.pack(side="left", fill="y")
self.left_name = tk.Label(self.frame_left)
self.left_name["text"] = f"Nome: {self.get_name(0)}\n{self.user_attribution(2)} SIG"
self.left_name["bg"] = color_blue
self.left_name["fg"] = "white"
self.left_name["font"] = self.font_main
self.left_name.pack(side="top", fill="both")
self.list_buttons = ["PAINEL PRINCIPAL", "SEGURANCA", "PESSOAS", "QUALIDADE", "MANUTENÇÃO", "PARADAS", "PRODUÇÃO"]
for name in self.list_buttons:
self.left_button = tk.Button(self.frame_left)
self.left_button["text"] = name
self.left_button["bg"] = color_blue
self.left_button["fg"] = "white"
self.left_button["activebackground"] = color_dgreen
self.left_button["activeforeground"] = "white"
self.left_button["font"] = self.font_main
#self.left_button["command"] = self.get_datatime(0) ###########################################
self.left_button["state"] = "disabled" # normal, disabled or active
self.left_button.pack(side="top", fill="both")
self.left_name = tk.Label(self.frame_left)
self.left_name["bg"] = color_blue
self.left_name.pack(side="top", fill="both", expand=True)
self.left_button = tk.Button(self.frame_left)
self.left_button["text"] = "HOME"
self.left_button["bg"] = color_blue
self.left_button["fg"] = "white"
self.left_button["activebackground"] = color_dgreen
self.left_button["activeforeground"] = "white"
self.left_button["font"] = self.font_main
#self.left_button["command"] = self.get_datatime(1) ###########################################
self.left_button["state"] = "disabled" # normal, disabled or active
self.left_button.pack(side="top", fill="both")
def get_datatime(self, dayhour):
self.now = datetime.now()
if dayhour == 0:
print(self.now.strftime("%d/%m/%Y"))
return self.now.strftime("%d/%m/%Y")
elif dayhour == 1:
print(self.now.strftime("%H:%M:%S"))
return self.now.strftime("%H:%M:%S")
else:
print(self.now.strftime("%d/%m/%Y_%H:%M:%S"))
return self.now.strftime("%d/%m/%Y_%H:%M:%S")
def user_attribution(self, attibution):
list_user = ["Digitalização", "Gestor", "Operador"]
return list_user[attibution]
def get_name(self, name):
list_name = ["Rodrigo Venturi", "Joao sem braco"]
return list_name[name]
# def menu_buttons_def(self):
# list_buttons = ["PAINEL PRINCIPAL", "SEGURANCA", "PESSOAS", "QUALIDADE", "MANUTENÇÃO", "PARADAS", "PRODUÇÃO"]
# for name in list_buttons:
class Modules(MainFrames):
def __init__(self, master=None):
super().__init__(master=None)
self.final_information = {"seisM": "", "Estacao": "", "Conjunto": "", "Elemento": "", "Hora Inicio": "", "Hora Fim": ""}
self.font_main = ("Calibri", 18)
self.frame_work = ttk.Frame(master)
self.frame_work.pack(side="left", fill="both", expand=True)
self.frame_6M = ttk.Frame(self.frame_work)
self.frame_6M.pack(side="top", fill="x")
# de duas colunas, essa e a coluna da esquerda
self.frame_tree = ttk.Frame(self.frame_work)
self.frame_tree.pack(side="left", fill="both")
self.frame_tree_top = ttk.Frame(self.frame_tree)
self.frame_tree_top.pack(side="top", fill="both")
self.frame_tree_bottom = ttk.Frame(self.frame_tree)
self.frame_tree_bottom.pack(side="top", fill="both")
# representa 5 linhas para matriz de botoes
self.frame_matrix_line1 = ttk.Frame(self.frame_work)
self.frame_matrix_line1.pack(side="top", fill="x")
self.frame_matrix_line2 = ttk.Frame(self.frame_work)
self.frame_matrix_line2.pack(side="top", fill="x")
self.frame_matrix_line3 = ttk.Frame(self.frame_work)
self.frame_matrix_line3.pack(side="top", fill="x")
self.frame_matrix_line4 = ttk.Frame(self.frame_work)
self.frame_matrix_line4.pack(side="top", fill="x")
self.frame_matrix_line5 = ttk.Frame(self.frame_work)
self.frame_matrix_line5.pack(side="top", fill="x")
self.frame_matrix_line6 = ttk.Frame(self.frame_work)
self.frame_matrix_line6.pack(side="top", fill="x")
self.frame_matrix_line7 = ttk.Frame(self.frame_work)
self.frame_matrix_line7.pack(side="top", fill="x")
self.frame_matrix_line8 = ttk.Frame(self.frame_work)
self.frame_matrix_line8.pack(side="top", fill="x")
self.frame_matrix_line9 = ttk.Frame(self.frame_work)
self.frame_matrix_line9.pack(side="top", fill="x")
self.frame_matrix_line10 = ttk.Frame(self.frame_work)
self.frame_matrix_line10.pack(side="top", fill="x")
# representa os frames para modulo de hora
self.frame_time_line1 = ttk.Frame(self.frame_work)
self.frame_time_line1.pack(side="top", fill="both")
self.frame_time_line2 = ttk.Frame(self.frame_work)
self.frame_time_line2.pack(side="top", fill="both")
self.frame_time_line3 = ttk.Frame(self.frame_work)
self.frame_time_line3.pack(side="top", fill="both")
# bloco de objetos gerericos que assumem caso algum modulo nao os chame/crie
self.open_text = tk.Text(self.frame_work)
self.buttons = []
self.buttons_tree = []
self.buttons_elem = []
self.sixM_button = None
self.tree_button = None
self.tree_entry = None
self.tree_entry2 = None
self.hour_string = None
self.past_hour = None
self.new_hour = None
self.buttons_hour = []
self.hour_button = None
self.buttons_end = []
self.confirm_cancel_button = None
self.confirm_cancel_button = None
self.conj_button = None
self.elem_button = None
self.buttons_conj = []
def modulo_6M(self): # cria os botoes de 6M
tk.Label(self.frame_6M).pack(side="top", fill="both", expand=True)
self.list_buttons = ["METODO", "MÃO DE OBRA", "MATERIA PRIMA", "MAQUINA", "MEDIÇÃO", "MEIO AMBIENTE"]
self.buttons = []
for name in self.list_buttons:
self.sixM_button = tk.Button(self.frame_6M)
self.sixM_button["text"] = name
self.sixM_button["height"] = 2
self.sixM_button["width"] = 10
self.sixM_button["bg"] = color_blue
self.sixM_button["fg"] = "white"
self.sixM_button["activebackground"] = color_dgreen
self.sixM_button["activeforeground"] = "white"
self.sixM_button["font"] = self.font_main
self.sixM_button["command"] = partial(self.func_botoes_6M, name)
self.sixM_button["state"] = "normal" # normal, disabled or active
self.sixM_button.pack(side="left", expand=True)
self.buttons.append(self.sixM_button)
def modulo_arvore(self): # cria os botoes de arvore da maquina
self.font_main = ("Calibri", 12)
self.list_buttons = {"Prea": "pre_a.png", "Preb": "pre_b.png",
"Prec": "pre_c.png", "Pred": "pre_d.png",
"Pree": "pre_e.png", "Pref": "pre_f.png",
"Preg": "pre_g.png", "Preh": "pre_h.png",
"Prei": "pre_i.png", "prej": "pre_j.png"}
self.buttons_tree = []
button_index = 0
for name in self.list_buttons:
if button_index < 5:
self.tree_button = tk.Button(self.frame_tree_top)
else:
self.tree_button = tk.Button(self.frame_tree_bottom)
self.photo = PhotoImage(file=self.list_buttons[name]).subsample(3,3)
self.tree_button["text"] = name
self.tree_button["image"] = self.photo
self.tree_button["height"] = 150
self.tree_button["width"] = 105
self.tree_button["bg"] = "white"
self.tree_button["fg"] = "black"
self.tree_button["activebackground"] = color_dgreen
self.tree_button["activeforeground"] = "white"
self.tree_button["font"] = self.font_main
self.tree_button["compound"] = "top"
self.tree_button["command"] = partial(self.func_botoes_tree, name)
self.tree_button["state"] = "normal" # normal, disabled or active
self.photo.photo = self.photo
self.tree_button.pack(side="left", expand=True)
self.buttons_tree.append(self.tree_button)
button_index += 1
self.tree_entry = tk.Text(self.frame_tree)
self.tree_entry["height"] = 6
self.tree_entry["width"] = 50
self.tree_entry["state"] = "disabled"
self.tree_entry["bg"] = color_grey
self.tree_entry.pack(side="left", padx=5, pady=(0, 10), fill="both", expand=True)
self.tree_entry2 = tk.Text(self.frame_tree)
self.tree_entry2["height"] = 6
self.tree_entry2["width"] = 10
self.tree_entry2["state"] = "disabled"
self.tree_entry2["bg"] = color_grey
self.tree_entry2.pack(side="left", padx=5, pady=(0, 10), fill="both", expand=True)
def modulo_text_campo_aberto(self):
self.open_text = tk.Label(self.frame_matrix_line1)
self.open_text.pack(side="top", fill="both", expand=True, pady=40)
self.open_text = tk.Label(self.frame_matrix_line1)
self.open_text["text"] = "Qual a condição insegura encontrada?"
self.open_text["font"] = self.font_main
self.open_text["anchor"] = "w"
self.open_text.pack(side="top", padx=10, fill="x", expand=True)
self.open_text = tk.Text(self.frame_matrix_line1)
self.open_text["height"] = 10
self.open_text["width"] = 50
self.open_text["bg"] = color_grey
self.open_text.pack(side="left", padx=10, anchor="s", fill="both", expand=True)
def modulo_hour_and_confirm(self):
self.past_hour = tk.Label(self.frame_time_line1)
self.past_hour["text"] = "Hora Inicio"
self.past_hour["font"] = ("Calibri", 13, "bold")
self.past_hour["anchor"] = "w"
self.past_hour.pack(side="left", fill="x", padx=18, pady=(20,0))
self.past_hour = tk.Label(self.frame_time_line1)
self.past_hour["text"] = "Hora Fim"
self.past_hour["font"] = ("Calibri", 13, "bold")
self.past_hour["anchor"] = "w"
self.past_hour.pack(side="left", fill="x", padx=95, pady=(20,0))
self.hour_string = tk.StringVar()
self.past_hour = ttk.Entry(self.frame_time_line2)
self.past_hour["width"] = 25
self.past_hour.pack(side="left", padx=20, fill="x")
self.new_hour = ttk.Entry(self.frame_time_line2)
self.new_hour["width"] = 25
self.new_hour["textvariable"] = self.hour_string
self.new_hour.pack(side="left", padx=20, fill="x")
self.list_buttons = {"Hora": "clock.png"}
self.buttons_hour = []
button_index = 0
for name in self.list_buttons:
self.hour_button = tk.Button(self.frame_time_line2)
self.photo = PhotoImage(file=self.list_buttons[name])
self.hour_button["image"] = self.photo
self.hour_button["height"] = 30
self.hour_button["width"] = 30
self.hour_button["bg"] = "white"
self.hour_button["activebackground"] = color_dgreen
self.hour_button["activeforeground"] = "white"
self.hour_button["font"] = self.font_main
self.hour_button["compound"] = "top"
self.hour_button["command"] = self.func_refresh_datatime
self.hour_button["state"] = "normal" # normal, disabled or active
self.photo.photo = self.photo
self.hour_button.pack(side="left", expand=True)
self.buttons_hour.append(self.hour_button)
button_index += 1
self.list_buttons = {"CONFIRMAR": "confirm.png", "CANCELAR": "cancel.png"}
self.buttons_end = []
button_index = 0
for name in self.list_buttons:
self.confirm_cancel_button = tk.Button(self.frame_time_line3)
self.photo = PhotoImage(file=self.list_buttons[name])
self.confirm_cancel_button["image"] = self.photo
self.confirm_cancel_button["height"] = 45
self.confirm_cancel_button["width"] = 140
self.confirm_cancel_button["text"] = name
self.confirm_cancel_button["bg"] = "white"
self.confirm_cancel_button["activebackground"] = "white"
self.confirm_cancel_button["font"] = self.font_main
self.confirm_cancel_button["compound"] = "right"
self.confirm_cancel_button["command"] = partial(self.func_confirm_cancel, name)
self.confirm_cancel_button["state"] = "normal" # normal, disabled or active
self.photo.photo = self.photo
self.confirm_cancel_button.pack(side="left", pady=(5,30), padx=20)
self.buttons_hour.append(self.confirm_cancel_button)
button_index += 1
def modulo_matriz_conjunto(self):
self.font_main = ("Calibri", 9)
self.list_buttons = ["botao1", "2", "3", "4", "5", "6"]
self.buttons_conj = []
button_index = 0
for name in self.list_buttons:
if button_index < 5:
self.conj_button = tk.Button(self.frame_matrix_line1)
elif 5 <= button_index < 10:
self.conj_button = tk.Button(self.frame_matrix_line2)
elif 10 <= button_index < 15:
self.conj_button = tk.Button(self.frame_matrix_line3)
elif 15 <= button_index < 20:
self.conj_button = tk.Button(self.frame_matrix_line4)
else:
self.conj_button = tk.Button(self.frame_matrix_line5)
self.conj_button["text"] = name
self.conj_button["height"] = 2
self.conj_button["width"] = 15
self.conj_button["bg"] = color_blue
self.conj_button["fg"] = "white"
self.conj_button["activebackground"] = color_dgreen
self.conj_button["activeforeground"] = "white"
self.conj_button["font"] = self.font_main
self.conj_button["command"] = partial(self.func_botoes_conjunto, name)
self.conj_button["state"] = "normal" # normal, disabled or active
self.conj_button.pack(side="left", expand=True)
self.buttons_conj.append(self.conj_button)
button_index += 1
def modulo_matriz_elemento(self):
self.list_buttons = ["1", "2", "3", "4", "5", "6"]
self.buttons_elem = []
button_index = 0
tk.Label(self.frame_matrix_line6, height="2").pack(side="top", fill="both", expand=True)
for name in self.list_buttons:
if button_index < 5:
self.elem_button = tk.Button(self.frame_matrix_line6)
elif 5 <= button_index < 10:
self.elem_button = tk.Button(self.frame_matrix_line7)
elif 10 <= button_index < 15:
self.elem_button = tk.Button(self.frame_matrix_line8)
elif 15 <= button_index < 20:
self.elem_button = tk.Button(self.frame_matrix_line9)
else:
self.elem_button = tk.Button(self.frame_matrix_line10)
self.elem_button["text"] = name
self.elem_button["height"] = 2
self.elem_button["width"] = 15
self.elem_button["bg"] = color_blue
self.elem_button["fg"] = "white"
self.elem_button["activebackground"] = color_dgreen
self.elem_button["activeforeground"] = "white"
self.elem_button["font"] = self.font_main
self.elem_button["command"] = partial(self.func_botoes_elemento, name)
self.elem_button["state"] = "normal" # normal, disabled or active
self.elem_button.pack(side="left", expand=True)
self.buttons_elem.append(self.elem_button)
button_index += 1
def func_botoes_conjunto(self, text_inside_button):
for button in self.buttons_conj:
button["relief"] = "raised"
button["bg"] = color_blue
for button in self.buttons_conj:
if button["text"] == text_inside_button:
button["bg"] = color_dgreen
button["relief"] = "sunken"
print(text_inside_button)
self.final_information["Conjunto"] = text_inside_button
self.modulo_matriz_elemento()
def func_botoes_elemento(self, text_inside_button):
for button in self.buttons_elem:
button["relief"] = "raised"
button["bg"] = color_blue
for button in self.buttons_elem:
if button["text"] == text_inside_button:
button["bg"] = color_dgreen
button["relief"] = "sunken"
print(text_inside_button)
self.final_information["Elemento"] = text_inside_button
def func_botoes_6M(self, text_inside_button):
for button in self.buttons:
button["relief"] = "raised"
button["bg"] = color_blue
for button in self.buttons:
if button["text"] == text_inside_button:
button["bg"] = color_dgreen
button["relief"] = "sunken"
print(text_inside_button)
self.final_information["seisM"] = text_inside_button
def func_botoes_tree(self, text_inside_button):
for button in self.buttons_tree:
button["relief"] = "raised"
button["bg"] = "white"
for button in self.buttons_tree:
if button["text"] == text_inside_button:
button["bg"] = color_dgreen
button["relief"] = "sunken"
print(text_inside_button)
self.final_information["Estacao"] = text_inside_button
self.modulo_matriz_conjunto()
def func_refresh_datatime(self):
self.hour_now = self.get_datatime(3)
self.hour_string.set(self.hour_now)
def func_campo_aberto(self):
print(self.open_text.get("1.0", "end-1c"))
def func_confirm_cancel(self, text_inside_button):
if text_inside_button == "CONFIRMAR":
self.final_information["Hora Fim"] = self.hour_string.get()
self.func_campo_aberto()
print(self.final_information)
else:
self.frame_work.destroy()
# aciona botão "painel principal"
root = tk.Tk()
root.geometry("1280x720")
test1 = Modules(root)
test1.modulo_6M()
test1.modulo_arvore()
#test1.modulo_matriz_conjunto()
#test1.modulo_matriz_elemento()
#test1.modulo_text_campo_aberto()
test1.modulo_hour_and_confirm()
root.mainloop()
EDIT2 :抱歉,我忘记了第一堂课(在Modules课之前)