Tkinter 如何改进Python代码以显示小部件

Tkinter 如何改进Python代码以显示小部件,tkinter,pycharm,widget,python-3.8,Tkinter,Pycharm,Widget,Python 3.8,Python代码使用PyCharm社区编译的tkinter,只显示空的应用程序表单,上面没有小部件。 PyCharm了解编译代码: 进程已完成,退出代码为0 代码如下: import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.pack() # create the application

Python代码使用PyCharm社区编译的tkinter,只显示空的应用程序表单,上面没有小部件。 PyCharm了解编译代码:

进程已完成,退出代码为0

代码如下:

import tkinter as tk
class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()

# create the application
app = Application()
app.master.title("Sumator")
app.master.minsize(width=640, height=96)

# start the program
app.mainloop()

def __init__(self, master=None):
    super().__init__(master)
    self.pack()
    self.create_widgets()

    def create_widgets(self):
        # create widgets
        self.firstNumberEntry = tk.Entry()
        self.plusSign = tk.Label(text="+")
        self.secondNumberEntry = tk.Entry()
        self.equalSign = tk.Label(text="=")
        self.resultLabel = tk.Label(text="Result...", bg="green", fg="white")
        self.calculateButton = tk.Button(text="Calculate")
        # place widgets
        self.firstNumberEntry.pack(side="left")
        self.plusSign.pack(side="left")
        self.secondNumberEntry.pack(side="left")
        self.resultLabel.pack(side="left")
        self.calculateButton.pack(side="left")

        tk.Button(text="Calculate", command=self.calculate)
        def calculate(self):
            first_value = float(self.firstNumberEntry.get())
            second_value = float(self.secondNumberEntry.get())
            result = first_value + second_value
            self.resultLabel.config(text=str(result), bg="green", fg="white")

如何改进代码、显示小部件并使用它们

这是您的实际代码吗,按照文件中的顺序排列?这正是我的实际代码,在我的文件中。可以说,我是Python、PyCharm和Tkinter的绝对初学者,这很好。这是我第一次尝试使用GUI小部件。(到目前为止,我只有Pyton和PyCharm的6个简单示例,但没有使用GUI小部件。)作为一项改进,我将代码的开头移到了结尾。。。。