1

I have the following code with several problems. The first of which is the createUser() command will run immediately after the window appears.

The second is my button tied to that command will not run the command itself (I have tested this by attempting to print a simple string back into IDLE).

The last problem I seem to have is my label does not update when I set the variable to different contents, even when .set() is used outside of the command.

root = tk.Tk()
root.title("Sign In Screen")

tk.Label(root,text="Username: ",font="Arial").grid(row=0,column=0)
userIDEntry = tk.Entry(root)
userIDEntry.grid(row=0,column=1)

tk.Label(root,text="Password: ",font="Arial").grid(row=1,column=0)
passwordEntry = tk.Entry(root)
passwordEntry.grid(row=1,column=1)

tk.Label(root,text="Confirm Password: ",font="Arial").grid(row=2,column=0)
passwordConfirm = tk.Entry(root)
passwordConfirm.grid(row=2,column=1)

def createAccount():
    if passwordEntry.get() == passwordConfirm.get():
        exampleFunction() #doesntwork
    else:
        var1.set("Passwords do not match!")
        root.update()

var1 = tk.StringVar()
var1.set("")
tk.Button(root,text="CREATE ACCOUNT",font="Arial",command=createAccount()).grid(row=3,column=0,columnspan=2)
tk.Label(root,textvariable=var1).grid(row=4,column=0,columnspan=2)
root.mainloop()

I hope someone can help me out, I'm trying to teach myself Tkinter and can't stop running into small issues like these.

1 Answers1

0

tkinter has some peculiar syntax (similar to threading). You should specify command=function and not command=function(). This will solve your first two issues. In fact after I ran it with some minor adjustments it worked (I think) to accomplish what you wanted for updating the variable as well!

import tkinter as tk

root = tk.Tk()
root.title("Sign In Screen")

tk.Label(root, text="Username: ", font="Arial").grid(row=0, column=0)
userIDEntry = tk.Entry(root)
userIDEntry.grid(row=0, column=1)

tk.Label(root, text="Password: ", font="Arial").grid(row=1, column=0)
passwordEntry = tk.Entry(root)
passwordEntry.grid(row=1, column=1)

tk.Label(root, text="Confirm Password: ", font="Arial").grid(row=2, column=0)
passwordConfirm = tk.Entry(root)
passwordConfirm.grid(row=2, column=1)

def createAccount():
    if passwordEntry.get() == passwordConfirm.get():
        print('thing') # your function was not included in post
    else:
        var1.set("Passwords do not match!")
        root.update()

var1 = tk.StringVar()
var1.set("")
tk.Button(root, text="CREATE ACCOUNT", font="Arial", command=createAccount).grid(row=3, column=0, columnspan=2) # removed ()
tk.Label(root, textvariable=var1).grid(row=4, column=0, columnspan=2)
root.mainloop()
Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • This "peculiar syntax" isn't unique to tkinter. It's just standard python for anytime that a function needs to be given a callable. – Bryan Oakley Mar 07 '19 at 20:58
  • @BryanOakley Hmm well that makes sense I guess. I've only encountered it with tkinter and threading, but I guess those are the only instances I've needed to make a function callable. Good to know! – Reedinationer Mar 07 '19 at 21:03
  • FWIW, it has nothing to do with threading. Sorting is a common place where you might see this syntax, where you pass a function to the sort command for sorting complex objects. – Bryan Oakley Mar 07 '19 at 22:06