Aug-04-2022, 04:03 AM
In this basic Python Tkinter code, I'm trying to bind certain functions to trigger upon either a UI button press or a keyboard key press.
I get that one asterisk lets the function take an unknown number of arguments, and a double asterisk acts as a dictionary with key values.
My questions are:
import tkinter as tk
from tkinter import ttk
main_window = tk.Tk()
main_window.title('Test UI')
# Change text with "Enter" then flush
def changeTextEnter():
text_label.configure(text=entry_bar.get())
entry_bar.delete(0, tk.END)
# Close program key function
def quitApp():
main_window.destroy()
# Enter Button
enter_button = ttk.Button(text='Enter', command=changeTextEnter)
enter_button.grid(row=0, column=0)
# Entry bar
entry_bar = ttk.Entry(width=30)
entry_bar.grid(row=0, column=1)
# Quit Button
quit_button = ttk.Button(text='Quit', command=main_window.destroy)
quit_button.grid(row=0, column=2)
# Text label
text_label = ttk.Label(text='TEST TEXT GOES HERE')
text_label.grid(row=1, column=0, columnspan=2)
# Bind enter key
main_window.bind('<Return>', changeTextEnter)
# Bind quit key
main_window.bind('<Escape>', quitApp)
main_window.mainloop()After a while of trial and error, it seems to work the way I want if I add an *randomVariable in the declarations of def changeTextEnter(*randomVariable): and def quitApp(*randomVariable):I get that one asterisk lets the function take an unknown number of arguments, and a double asterisk acts as a dictionary with key values.
My questions are:
- Why do I need a parameter in those functions at all?
- How does the variable
*randomVariableget used, since it seems like I'm not actually using/ assigning anything torandomVariableanywhere within the function.
- Why does the function not work as intended without the asterisk before the variable?
