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

时间:2018-10-19 19:38:28

标签: 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()

1 个答案:

答案 0 :(得分:4)

您将要离开mainloop,然后退出:

g.exit_GUI()

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

def info_popup(self):
    pass