关于Python中的tkinter

关于Python中的tkinter,python,tkinter,Python,Tkinter,我只想使用tkinter定义一个类,但是在类中,根目录上无法显示按钮。这就是代码 from tkinter import * from tkinter import messagebox class EasyCalculator: def __init__(self): self.root = Tk() self.root.geometry('600x600') self.root.title('Calculator')

我只想使用
tkinter
定义一个类,但是在类中,
根目录上无法显示按钮。这就是代码

from tkinter import *
from tkinter import messagebox


class EasyCalculator:
    def __init__(self):
        self.root = Tk()
            self.root.geometry('600x600')
            self.root.title('Calculator')

            
        def Interface(self):    
            Button1 = Button(self.root,text='hi',bg='yellow')
            Button1.pack()
            

        def show(self):
            self.root.mainloop()

if __name__=='__main__':
    Calculator1 = EasyCalculator()
    Calculator1.show()

您的问题是从未调用
Calculator1.interface()
,因此按钮无法显示

在您的
Calculator1.show()
之前添加
Calculator1.interface()
,它应该可以:

从tkinter导入*
从tkinter导入消息框
类简易计算器:
定义初始化(自):
self.root=Tk()
self.root.geometry('600x600')
self.root.title('Calculator')
def接口(自):
Button1=按钮(self.root,text='hi',bg='yellow')
按钮1.pack()
def显示(自我):
self.root.mainloop()
如果“名称”=“\uuuuuuuu主要”:
计算器1=EasyCalculator()
计算器1.接口()
计算器1.show()

您的问题是,
Calculator1.interface()
从未被调用,因此按钮无法出现

在您的
Calculator1.show()
之前添加
Calculator1.interface()
,它应该可以:

从tkinter导入*
从tkinter导入消息框
类简易计算器:
定义初始化(自):
self.root=Tk()
self.root.geometry('600x600')
self.root.title('Calculator')
def接口(自):
Button1=按钮(self.root,text='hi',bg='yellow')
按钮1.pack()
def显示(自我):
self.root.mainloop()
如果“名称”=“\uuuuuuuu主要”:
计算器1=EasyCalculator()
计算器1.接口()
计算器1.show()

您只需调用接口即可

from tkinter import *
from tkinter import messagebox


class EasyCalculator:
        def __init__(self):
            self.root = Tk()
            self.root.geometry('600x600')
            self.root.title('Calculator')

            
        def Interface(self):    
            Button1 = Button(self.root,text='hi',bg='yellow')
            Button1.pack()
            

        def show(self):
            self.root.mainloop()

if __name__=='__main__':
    Calculator1 = EasyCalculator()
    Calculator1.Interface()
    Calculator1.show()

这是因为当您调用
EasyCalculator
类时,只会执行
\uuuu init\uuuu(self)

或者,您可以编辑
\uuuuu init\uuuuuuuu(self)
,如下所示:

        def __init__(self):
            self.root = Tk()
            self.root.geometry('600x600')
            self.root.title('Calculator')
            Button1 = Button(self.root,text='hi',bg='yellow')
            Button1.pack()


您只需调用接口即可

from tkinter import *
from tkinter import messagebox


class EasyCalculator:
        def __init__(self):
            self.root = Tk()
            self.root.geometry('600x600')
            self.root.title('Calculator')

            
        def Interface(self):    
            Button1 = Button(self.root,text='hi',bg='yellow')
            Button1.pack()
            

        def show(self):
            self.root.mainloop()

if __name__=='__main__':
    Calculator1 = EasyCalculator()
    Calculator1.Interface()
    Calculator1.show()

这是因为当您调用
EasyCalculator
类时,只会执行
\uuuu init\uuuu(self)

或者,您可以编辑
\uuuuu init\uuuuuuuu(self)
,如下所示:

        def __init__(self):
            self.root = Tk()
            self.root.geometry('600x600')
            self.root.title('Calculator')
            Button1 = Button(self.root,text='hi',bg='yellow')
            Button1.pack()


你没有调用接口函数,所以按钮没有初始化。非常感谢你没有调用接口函数,所以按钮没有初始化。非常感谢你,非常感谢