Python Tkinter按钮

Python Tkinter按钮,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在尝试编写一个程序,用户可以使用Tkinter在Python中创建一个时间表。现在,我正在尝试为“主菜单”设置GUI,用户可以单击该按钮并打开另一个窗口或下一个任务所需的任何内容。但是,我的程序会自动运行函数下的内容。我该如何着手解决这个问题?当我尝试用谷歌搜索时,我找不到任何结果,因为很多人没有展示如何将GUI定义为一个类 from tkinter import * from tkinter.filedialog import askopenfilename def loadTable

我正在尝试编写一个程序,用户可以使用Tkinter在Python中创建一个时间表。现在,我正在尝试为“主菜单”设置GUI,用户可以单击该按钮并打开另一个窗口或下一个任务所需的任何内容。但是,我的程序会自动运行函数下的内容。我该如何着手解决这个问题?当我尝试用谷歌搜索时,我找不到任何结果,因为很多人没有展示如何将GUI定义为一个类

from tkinter import *
from tkinter.filedialog import askopenfilename


def loadTable():
    filename = askopenfilename(filetypes=[("allfiles","*")])
def addCourse():
    print("Insert functionality here")
def addTime():
    print("Insert functionality here")
def resetTable():
    print("Insert functionality here")
def addComment():
    print("Insert functionality here")
def viewTable():
    print("Insert functionality here")


class menu():
    app = Tk()
    app.geometry("800x600+200+200")
    app.title("Time Tracker")
    #load the buttons for UI
    bLoad = Button(app, text = "Load Table", command = loadTable())
    bCourse = Button(app, text = "Add Course", command = addCourse())
    bTime = Button(app, text = "Add Time", command = addTime())
    bReset = Button(app, text = "Reset Time", command = resetTable())
    bComment = Button(app, text = "Add Comment", command = addComment())
    bView = Button(app, text = "View Table", command = viewTable())
    bLoad.pack(side="top", anchor = "w", padx=15, pady=35)
    bCourse.pack(side="top", anchor = "w", padx=15, pady=35)
    bTime.pack(side="top", anchor = "w", padx=15, pady=35)
    bReset.pack(side="top", anchor = "w", padx=15, pady=35)
    bComment.pack(side="top", anchor = "w",  padx=15, pady=35)
    bView.pack(side="top", anchor = "w", padx=15, pady=35)

if __name__ == '__main__':
    mainloop()

这是因为您用按钮调用了功能。它不会等到按下按钮。这些函数是自动调用的

试试这个

bLoad = Button(app, text = "Load Table", command = loadTable)
bCourse = Button(app, text = "Add Course", command = addCourse)
bTime = Button(app, text = "Add Time", command = addTime)
bReset = Button(app, text = "Reset Time", command = resetTable)
bComment = Button(app, text = "Add Comment", command = addComment)
bView = Button(app, text = "View Table", command = viewTable)

这是因为您用按钮调用了功能。它不会等到按下按钮。这些函数是自动调用的

试试这个

bLoad = Button(app, text = "Load Table", command = loadTable)
bCourse = Button(app, text = "Add Course", command = addCourse)
bTime = Button(app, text = "Add Time", command = addTime)
bReset = Button(app, text = "Reset Time", command = resetTable)
bComment = Button(app, text = "Add Comment", command = addComment)
bView = Button(app, text = "View Table", command = viewTable)

需要更多的代码。您提供的代码没有传达太多信息,无法生成与您所获得的相同的不希望的行为。@Ashish Nitin Patil基本上我希望按钮在单击时调用函数。但是,当我在这种状态下运行程序时,所有函数都会运行,因此我的空闲shell会获取所有打印语句,并弹出文件管理器。我基本上想在这一点上解决这个问题。然而,我计划可能让这些按钮中的一个打开一个新窗口,向我的表中添加信息。需要更多的代码。您提供的代码没有传达太多信息,无法生成与您所获得的相同的不希望的行为。@Ashish Nitin Patil基本上我希望按钮在单击时调用函数。但是,当我在这种状态下运行程序时,所有函数都会运行,因此我的空闲shell会获取所有打印语句,并弹出文件管理器。我基本上想在这一点上解决这个问题。但是,我计划可能使这些按钮中的一个打开一个新窗口,向我的表格中添加信息。@WorldDominator如果对您有帮助,请接受答案对不起,我很忙。成功了。谢谢我想我以前已经改变了这一点,并没有发现任何区别。尽量不要使用hit&trial。我通常在我不熟悉的时候做。你建议只看语法还是什么?即使你在尝试一些不熟悉的东西,也要试着理解你在做什么。这样,在将来,您将能够快速评估您的代码,以检查错误所在。@WorldDominator如果对您有帮助,请接受答案。对不起,我很忙。成功了。谢谢我想我以前已经改变了这一点,并没有发现任何区别。尽量不要使用hit&trial。我通常在我不熟悉的时候做。你建议只看语法还是什么?即使你在尝试一些不熟悉的东西,也要试着理解你在做什么。这样,在将来,您将能够快速评估代码,以检查错误所在。