Python 3.x 如何在tkinter中访问类内的按钮

Python 3.x 如何在tkinter中访问类内的按钮,python-3.x,tkinter,Python 3.x,Tkinter,我正在使用Python 3.8.0和Tkinter 8.6。 我试图通过button.config方法访问button one,但我不知道如何在类外访问该按钮 我尝试为按钮一指定一个名称,然后尝试app.name.config(),但没有成功,Python也无法识别该名称 将tkinter作为tk导入 从tkinter导入字体 类应用程序(tk.Frame): def uuu init uuu(self,master=None): tk.Frame.\uuuuu init\uuuuuuu(自,

我正在使用Python 3.8.0和Tkinter 8.6。 我试图通过
button.config
方法访问button one,但我不知道如何在类外访问该按钮

我尝试为按钮一指定一个名称,然后尝试
app.name.config()
,但没有成功,Python也无法识别该名称

将tkinter作为tk导入
从tkinter导入字体
类应用程序(tk.Frame):
def uuu init uuu(self,master=None):
tk.Frame.\uuuuu init\uuuuuuu(自,主)
self.grid()
self.theWidgets()
定义小部件(自身):
self.one=tk.Button(self,text='New Game',command=onePressed())
self.one.grid(行=0,列=0,padx=100)
self.two=tk.Button(self,text='Load Game')
self.two.grid(行=1,列=0,padx=100,pady=10)
self.three=tk.Button(self,text='Quit',command=self.Quit,anchor=tk.W,font='Helvetica 18 bold')
self.three.grid(行=2,列=0,padx=100,pady=10)
app=应用程序()
def onePressed():
#将按钮1的状态更改为tk.DISABLED
app.mainloop(

)
控制类成员内部状态的代码(例如按钮)通常属于类内部

按钮按下的动作应该定义为类的一部分

(与其他情况一样,偶尔也会有例外,但目前请遵循上述规定)

有关更多详细信息,请参阅en.wikipedia.org/wiki/enclosuration(计算机编程)

在您的示例中,您只需在类中移动button press函数,然后将其与按钮关联(与您将quit与按钮3关联的方式相同)


当你说你试图在课堂外访问这个按钮时,你想用这个按钮做什么。例如,您是否正在尝试确定按下了哪个按钮?我正在尝试将按钮的状态更改为禁用,以便在单击按钮时消失。您不会在类外更改它。该函数属于类内。但是如果我不能在类外编辑它,按钮会有什么用呢?我不明白你为什么要在类外编辑它
import tkinter as tk
from tkinter import font
class Application(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.theWidgets()

    def theWidgets(self):
        self.one = tk.Button(self, text='New Game', command=self.onePressed)
        self.one.grid(row=0,column=0,padx=100)
        self.two = tk.Button(self, text='Load Game')
        self.two.grid(row=1,column=0,padx=100,pady=10)
        self.three = tk.Button(self, text='Quit', command=self.quit, anchor=tk.W, font='Helvetica 18 bold')
        self.three.grid(row=2, column=0, padx=100, pady=10)

    def onePressed(self):
        self.one.config(state="disabled")

app = Application()
app.mainloop()