gpt4 book ai didi

python - 使用第二个按钮更改按钮状态

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:18 26 4
gpt4 key购买 nike

我想通过按下另一个按钮将一个按钮的状态更改为正常。

import tkinter as tk
from tkinter import ttk, StringVar
from tkinter.constants import DISABLED

LARGE_FONT =('Verdana',12)

class Auswertung(tk.Tk):

def __init__(self,*args, **kwargs):

tk.Tk.__init__(self,*args,**kwargs)
container = tk.Frame(self)
container.grid(row=0, column=1, padx=10, pady=10)

container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

self.frames = {}
for F in (Datenwahl,):
frame = F(container,self)

self.frames[F] = frame

frame.grid(row=0, column=0, sticky='nsew')

self.show_frame(Datenwahl)

def show_frame(self,cont):

frame = self.frames [cont]
frame.tkraise()

def enable_button2(self):
self.button2.config(sate = NORMAL)


class Datenwahl(tk.Frame):



def __init__(self, parent, controller):
tk.Frame.__init__(self,parent,)



label=tk.Label(self, text='Start Page', font=LARGE_FONT)
label.grid(row=0, column=1, padx=10, pady=10 ,sticky= 'ew')

button1 = tk.Button(self, text= 'Visit Page 1',
command= lambda : controller.enable_button2())
button1.grid(row=1, column=1, padx=10, pady=10)

button2 = tk.Button(self, text= 'Visit Page 2', state=DISABLED,
command= lambda : controller.show_frame(PageTwo))
button2.grid(row=2, column=1, padx=10, pady=10)


app = Auswertung()
app.mainloop()

这是完整的错误消息:

Traceback (most recent call last):
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\pasca\OneDrive\Dokumente\Privat\Python lernen\nested\tested.py", line 50, in <lambda>
command= lambda : controller.enable_button2())
File "C:\Users\pasca\OneDrive\Dokumente\Privat\Python lernen\nested\tested.py", line 34, in enable_button2
self.button2.config(sate = NORMAL)
File "C:\Users\pasca\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2101, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'button2'

我尝试了不同的方法来解决这个问题,但大多数情况下它要么给出相同的错误,要么对按钮状态没有任何作用。我期待我们的帮助。

最佳答案

您需要通过添加 self 使 button2 成为 Datenwahl 的类属性,而不是创建局部变量:

class Datenwahl(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self,parent,)
...
self.button2 = tk.Button(self, text= 'Visit Page 2', state="disabled",
command= lambda : controller.show_frame(PageTwo))
self.button2.grid(row=2, column=1, padx=10, pady=10)

然后您可以在 Auswertung 实例中访问 button2:

class Auswertung(tk.Tk):

...

def enable_button2(self):
self.frames[Datenwahl].button2.config(state = "normal")

关于python - 使用第二个按钮更改按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58316916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com