Python Forum
Whys is asterisk and random variable necessary in these functions?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whys is asterisk and random variable necessary in these functions?
#1
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.

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:
  1. Why do I need a parameter in those functions at all?
  2. How does the variable *randomVariable get used, since it seems like I'm not actually using/ assigning anything to randomVariable anywhere within the function.
  3. Why does the function not work as intended without the asterisk before the variable?
Reply


Messages In This Thread
Whys is asterisk and random variable necessary in these functions? - by rrowhe4d - Aug-04-2022, 04:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 966 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  trying to input a variable using random.choice python63 9 3,840 Aug-13-2020, 05:37 PM
Last Post: python63
  Help removing asterisk item in a nested list. bmcguire 3 2,714 Apr-06-2020, 02:35 PM
Last Post: snippsat
  append one random intergar variable Coastal 5 2,813 Dec-18-2019, 09:18 PM
Last Post: ichabod801
  random variable function? Novice_fanatic 4 2,869 Oct-08-2019, 01:22 PM
Last Post: jefsummers
  Guidance in Creating random output for a variable. Livne_ye 1 2,591 May-04-2019, 01:18 PM
Last Post: Yoriz
  How do you make functions that take a variable that is not defined? magic 6 4,586 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  multiprocess passing multiple arguments double asterisk pic8690 1 5,388 Oct-23-2016, 08:51 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020