1
import Tkinter as tk
from functools import partial

pad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], ["C", 0, "S"]]
passcode = ""

def append_passcode(value):
    global passcode
    if len(passcode) == 4:
        passcode = passcode[1:]
    passcode += value

def clear():
    global passcode
    passcode = ""

def submit():
    global passcode
    if passcode == "1234":
        msgBox.showinfo("Login Attempt", "Successful")
        passcode = ""
    else:
        msgBox.showinfo("Login Attempt", "Failed")
        passcode = ""

main_window = tk.Tk()

btns = []
row_placement = 0
for line in pad:
    col_placement = 0
    for number in line:
        btn_command = partial(append_passcode, str(number))
        btn = tk.Button(main_window, text=str(number), width=10, command=btn_command)
        btns.append(btn)
        btn.grid(row=row_placement, column=col_placement)
        col_placement += 1
    row_placement += 1

#Setting C to clear the passcode function
btns[-3].config(command=clear)

#Setting S to submit passcode
btns[-1].config(command=submit)

I'm writing an numpad application where user must enter the correct 4 digit code to access application. when the test code runs, the GUI does not appear. What needs to be done to get this code to function?

1
  • If my answer help you, could you please press the green tick Commented Jul 19, 2016 at 3:40

1 Answer 1

1

Here's what you've done wrong:

  • Misspelled tkinter
  • Not included

    main_window.mainloop()
    

Try this code

import tkinter as tk
from functools import partial

pad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], ["C", 0, "S"]]
passcode = ""

def append_passcode(value):
    global passcode
    if len(passcode) == 4:
        passcode = passcode[1:]
    passcode += value

def clear():
    global passcode
    passcode = ""

def submit():
    global passcode
    if passcode == "1234":
        msgBox.showinfo("Login Attempt", "Successful")
        passcode = ""
    else:
        msgBox.showinfo("Login Attempt", "Failed")
        passcode = ""

main_window = tk.Tk()

btns = []
row_placement = 0
for line in pad:
    col_placement = 0
    for number in line:
        btn_command = partial(append_passcode, str(number))
        btn = tk.Button(main_window, text=str(number), width=10, command=btn_command)
        btns.append(btn)
        btn.grid(row=row_placement, column=col_placement)
        col_placement += 1
    row_placement += 1

#Setting C to clear the passcode function
btns[-3].config(command=clear)

#Setting S to submit passcode
btns[-1].config(command=submit)

main_window.mainloop()

Now your window should appear.

Hope this helps and feel free to comment any questions.

Sign up to request clarification or add additional context in comments.

1 Comment

tkinter may not be misspelled- they have the correct spelling for python2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.