Hello,
I have a test code that contains two classes. Each class corresponds to a different window. The first class ‘App’ is the main window. The ‘UserForm’ class corresponds to the second window. The second window opens up after the ‘Create user’ button is pressed in the first window. I have gone ahead and added the ‘Return’ bind option key so that a I can enable it by pressing the Enter key
(in addition to enabling it with the left-side mouse click). I would like to do the same for the ‘Submit’ button in the other window but appears to not be accepting the same set up. Note that the callback method for the Submit button is: self.destroy, and not an actual developer defined method. This is where the issue appears to lie.
Does someone know how to set up a bind key when the callback method is not a developer defined method but is an internal method?
Any help would be appreciated.
Here is the code:
import tkinter as tk
from collections import namedtuple
User = namedtuple("User", ["username", "password", "user_type"])
class UserForm(tk.Toplevel): # Secondary window
def __init__(self, parent, user_type):
super().__init__(parent)
tk.Tk.title(self,'Login') # Window title
# Create two attributes of type 'StringVar()
self.username = tk.StringVar()
self.password = tk.StringVar()
self.user_type = user_type
label = tk.Label(self, text = "Create a new " + user_type.lower(), font = 'Helvetica 9 bold')
entry_name = tk.Entry(self, textvariable = self.username)
entry_pass = tk.Entry(self, textvariable = self.password, show = "*")
btn = tk.Button(self, text = "Submit", bg = 'light blue', command = self.destroy)
entry_name.focus_set() # Put cursor in 'Username' cell entry at start up
btn.bind("<Return>") # Add 'Enter' key as additional input to button
tk.Label(self, text = "Username:").grid(row = 1, column = 0)
tk.Label(self, text = "Password:").grid(row = 2, column = 0)
# Display widgets
label.grid(row = 0, columnspan = 2, pady = 10)
entry_name.grid(row = 1, column = 1, padx = 15)
entry_pass.grid(row = 2, column = 1, padx = 15)
btn.grid(row = 3, pady = 10, columnspan = 2)
def open(self):
self.grab_set()
self.wait_window()
username = self.username.get()
password = self.password.get()
return User(username, password, self.user_type)
class App(tk.Tk): # main window
def __init__(self):
super().__init__()
tk.Tk.title(self,'Selection') # Window title
tk.Tk.geometry(self, '220x170') # Window size
# Tuple of strings - RadioButton labels
user_types = ("Administrator", "Supervisor", "Regular user")
# Create attribute of type StringVar()
self.user_type = tk.StringVar(self, user_types[0])
# Create label and display it
label = tk.Label(self, text = "Select user type:", font = 'Helvetica 9 bold')
label.pack(padx = 10, pady = 10)
# List comprehension to create radio click selection buttons
radios = [tk.Radiobutton(self, text = t, value = t, variable = self.user_type) for t in user_types]
# Display (pack) and configure the alignment of the radio click buttons
for radio in radios:
radio.pack(padx = 10, anchor = tk.W)
# Create button and display it
btn = tk.Button(self, text = "Create user", bg = 'light blue', command = self.open_window)
btn.bind("<Return>", self.open_window) # Add 'Enter' key as additional input
btn.pack(pady = 10)
def open_window(self, *args):
window = UserForm(self, self.user_type.get())
user = window.open()
print(user)
if __name__ == "__main__":
app = App()
app.mainloop()