python - Tkinter 模块 - 程序将只运行然后停止并且不会打开窗口

标签 python python-3.x tkinter

我需要帮助,我正在做一个预算计算器并第一次使用 tkinter,想知道为什么它不起作用......

当我运行它时,它会立即结束,当我将 root = Tk() 放在最后时,它会出现错误。

我真的需要帮助,我的代码如下...

from time import sleep
from tkinter import * 
from tkinter import messagebox, ttk, Tk

root = Tk()

class GUI():

    def taskbar(self):

        menu = Menu()
        file = Menu(menu)
        file.add_command(label="Exit", command=self.exit_GUI)
        file.add_command(label = "Information", command=self.info_popup)        

    def Main_Menu(self):
        topFrame = Frame(root)
        topFrame.pack()
        bottomFrame = Frame(root)
        bottomFrame.pack(side=BOTTOM)

        Income_button = Button(topFrame, text="Enter your incomes", command=self.Income)
        Expense_button = Button(topFrame, text="Enter your expenses", command=self.Expense)
        Total_button = Button(bottomFrame, text="View Results", command=self.Total)
        Income_button.pack()
        Expense_button.pack()
        Total_button.pack()

    def Income(self):
        pass

    def Expense(self):
        pass

    def Total(self):
        pass

    def exit_GUI(self):
        exit()

    def info_popup():
        pass

g = GUI()
g.Main_Menu()
g.taskbar()
g.Income()
g.Expense()
g.Total()
g.exit_GUI()
g.info_popup()

root.mainloop()

最佳答案

在进入 mainloop 之前,您将退出:

g.exit_GUI()

该方法正在调用标准 exit() 并停止整个脚本。删除或注释掉上面的调用。您还需要将 self 作为参数添加到 info_popup 以使脚本运行:

def info_popup(self):
    pass

关于python - Tkinter 模块 - 程序将只运行然后停止并且不会打开窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52898919/

相关文章:

python - 为什么我不能在 Python 中更改类的属性

python - 如何在 python 服务器中的某个时间间隔后调用函数?

python - 如何将卷积层添加到自定义估计器

python - 如何知道 Tkinter.Frame 中有多少行/列

python - 使用 Python 进行套接字编程 : Get the port of a server

python - Tkinter 机智 while 循环 (python 3)

python - 迭代函数参数

python - 图像到文本python

python-3.x - 使用机器学习算法时无法将字符串转换为 float (python 3) (Anaconda)

python - 为什么使用 `iter` 函数的双参数形式而不是 while 循环?