WorldDominator
WorldDominator

Reputation: 141

Tkinter Buttons

I'm trying to write a program where a user can create a time table in Python using Tkinter. Right now, I'm trying to setup the GUI for basically the "main menu" where the user can click the button and bring up another window or whatever is necessary for the next task. However, my program automatically runs what I have under the functions. How do I go about fixing this? I can't find any results when I try to google because many don't show how to define the GUI as a class.

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()

Upvotes: 0

Views: 2986

Answers (2)

Ray Kong
Ray Kong

Reputation: 36

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)

Upvotes: 0

Alok
Alok

Reputation: 2689

It is because you have called the function with your button. It doesn't wait till the button is pressed. The functions are called automatically.

Try this

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)

Upvotes: 4

Related Questions