Python Forum
Problem passing argument to functionin inTkinter frame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem passing argument to functionin inTkinter frame
#1
This is the method I have inside a Tkinter frame which I am trying to call using key_press() but get the error.


ERROR MESSAGE
Error:
Traceback (most recent call last): File "/home/eric/Python_Projects/Window1.py", line 53, in <module> key_press() TypeError: key_press() takes exactly 1 argument (0 given)
I am struggling to find out which argument to pass.
I am probably making a rookie error here but cannot see the wood from the trees so need some help please.

The function runs ok in IDLE without the call.
CODE SNIPPET

main_window =Tk()

def key_press(event):
   
    if event.keysym == "1":   #if key pressed = 1 then go to next frame
        print ("Next")
    elif event.keysym == "9": # if key pressed = 9 then quit application.
        print ("Quit")
       
    main_window.bind("<Keys>", key_press)

key_press() #call key_press function


main_window.mainloop()
buran write Sep-11-2023, 12:27 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
You need to bind the callback function, not call it directly. in that case when you press a key, it will fire an event that will be passed automatically as an argument
import tkinter as tk

main_window = tk.Tk()
 
def key_press(event):
    
    if event.keysym == "1":   #if key pressed = 1 then go to next frame
        print ("Next")
    elif event.keysym == "9": # if key pressed = 9 then quit application.
        print ("Quit")

main_window.bind("<Key>", key_press)
main_window.mainloop()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank You, understand now works fine.
Reply
#4
You bind the keypress event once, not each time the event occurs.

The event name is <Key>, not <Keys>.

When you called key_press() you did not pass an event argument.

You should not use wildcard imports (from tkinter import *).

Next time you post, post the error message and a complete, runnable example. At least runnable enough to demonstrate the problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug [PyQt] PyQt5 _translate not passing argument Lahearle 2 799 Jul-23-2023, 11:05 AM
Last Post: Axel_Erfurt
  Syntax Error: Positional argument follows keyword argument Rama02 3 4,133 Feb-09-2021, 06:10 PM
Last Post: deanhystad
  Problem passing parameters between windows kenwatts275 2 1,736 Apr-12-2020, 02:43 PM
Last Post: kenwatts275
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 9,043 Sep-30-2019, 07:30 AM
Last Post: Maksim
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 11,176 Apr-18-2019, 05:36 PM
Last Post: atlass218
  [Tkinter] Frame size only works if frame is empty(Solved) Tuck12173 7 6,482 Jan-29-2018, 10:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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