Im using tkinter and pynput. I have a button to select a trigger key after the user press the key I want to show the ord of the pressed key in a label this is the error: ord() expected string of length 1, but KeyCode found
and here is the code:
TriggerKey = Button(win, text = "Set a trigger key", command = Key_listener)
TriggerKey.place(x = 70, y = 70,)
This is the Listen function:
def Key_listener():
with Listener (on_press=trigger_Key, on_release=release) as trigger:
trigger.join()
and here is where I think the problem is:
def trigger_Key(Key):
TriggerKey = Key
print(TriggerKey) #prints the pressed button for a test
ord_key = ord(TriggerKey)
trigger_key_label.config(text= ord_key)
If you used it with tkinter,it will block your code.
Change your function Key_listener:
def Key_listener():
trigger = Listener (on_press=trigger_Key, on_release=release)
trigger.start()
About your error:
in the trigger_Key,key is a Keycode function.You need to use ord(Key.char).
Related
from tkinter import *
def quit_1(event):
print("you pressed Win_L")
root.quit()
def quit_2(event):
print("you pressed Win_L+D")
root.quit()
root = Tk()
root.bind('<Win_L>', quit_1) #working
root.bind('<Win_L-D>', quit_2) #not working
root.mainloop()
How I bind Win_L + D event to a funcction
commenting line
root.bind('Win_L-D',quit_2) runs the program
To bind the Win_L + D key combination to the quit_2 function, you need to modify the event string in the bind method. Instead of using '<Win_L-D>', you need to use '<Win_L>d':
root.bind('<Win_L>', quit_1)
root.bind('<Win_L>d', quit_2)
This is because Win_L is a modifier key and cannot be used in combination with other keys. When you press Win_L + D, the Windows operating system generates a new key code for the combination. In Tkinter, this code is represented by d (the d key), so you need to use it in the event string.
With this change, pressing the Win_L + D key combination will print "you pressed Win_L+D" to the console and quit the root window.
this works for normal keys like [q,w,e,r,1,2,3] but i cant make it work with keys like with Shift,Enter,Backspace etc,any idea how to make it work?
from tkinter import *
root=Tk()
root.geometry("200x200")
def func1(event):
print(f'{event.char} key was pressed')
def func2(event):
print(f'{event.char} key was released')
root.bind("<KeyPress>",func1)
root.bind("<KeyPress>",func2)
root.mainloop()
From the docs:
.char:
If the event was related to a KeyPress or KeyRelease for a key that produces a regular ASCII character, this string will be set to that character. (For special keys like delete, see the .keysym attribute, below.)
Since here you are trying to get a special key like Shift, Caps and so on, you can use keysym which will provide the key name.
Based on your comments, I see that you were expecting for a function that will trigger when the Shift key was pressed and released. This can be achieved by using KeyPress and KeyRelease before the key name:
def func1(event):
print(f'{event.keysym} key was pressed')
def func2(event):
print(f'{event.keysym} key was released')
root.bind("<KeyPress-Shift_L>",func1)
root.bind("<KeyRelease-Shift_L>",func2)
Some useful reference links:
54.6. Writing your handler: The Event class
54.5. Key names
you have to use these to .bind function :
root.bind("<Alt-s>",fAlt) # Alt+s pressed
root.bind("<Control-c>",func) # C character + Ctrl
root.bind("<Alt>",func1) # The Alt key is held
root.bind("<Control>",func2) # The Ctrl key is held
root.bind("<Shift>",func3) # The Shift key is held
root.bind("<KeyPress>",func4)
root.bind('<Left>', leftKey)
root.bind('<Right>', rightKey)
root.bind('<Up>', upKey)
root.bind('<Down>', downKey)
I would like to do this for the Enter key but not the Return key:
root.bind('<Return>',func)
If you are not clear on the difference between the enter key and the return key
http://en.wikipedia.org/wiki/Enter_key
I would appreciate help, thank you!
One way to find out what's the correct key binding is to create a key binding for all keys and printing the keysym of the event. Now, just hit the key you want to bind the event to and see what it prints.
import Tkinter
root = Tkinter.Tk()
def func(event):
print event.keysym
root.bind("<Key>", func)
root.mainloop()
When pressing the Enter key, this will print KP_Enter, so your binding should be
root.bind('<KP_Enter>', func)
I have two buttons on my interface. I want both of them to be able to call their respective functions when I either click on them or a hit the Enter Key.
The problem I'm having is that only the last button in the traveral focus gets activated when I hit the Enter Key, even if the preceeding one has the focus. What can I do to resolve this problem.
Useful answer are welcome and appreciated.
This is the problem in question:
from tkinter import *
w = Tk()
def startProgram(event = None):
print('Program Starting')
def readyContent(event = None):
print('Content being prepared')
# Buttons
Button(text='Prepare', command=readyContent).grid(row=10,column=2)
w.bind('<Return>',readyContent) # Binds the Return key to a Function
Button(text='Start', command=startProgram).grid(row=10,column=3)
w.bind('<Return>',startProgram) # Binds the Return key to a Function
w.mainloop()
When you click on the Prepare or Start button, in return you get either Content being prepared or Program Starting repectively. Nothing like that happens when you use the Tab Key to give focus to one button or the other. Even if the focus is on the Prepare button, when you hit Enter you get: Program Starting
This is the solution to my problem.
I hope it helps anyone else having the same problem as me.
from tkinter import *
w = Tk()
def startProgram(event = None):
print('Program Starting')
def readyContent(event = None):
print('Content being prepared')
# Buttons
btn1 = Button(text='Prepare', command=readyContent)
btn1.grid(row=10,column=2)
btn1.bind('<Return>',readyContent) # Binds the Return key to a Function
btn2 = Button(text='Start', command=startProgram)
btn2.grid(row=10,column=3)
btn2.bind('<Return>',startProgram) # Binds the Return key to a Function
w.mainloop()
Have a good day! :)
If I create an entry box like so:
myentry = Entry ()
myentry.place (x = 54,y = 104)
the value that the user enters is a string value. What do I have to add so that the entry is a float? I have tried to write "float" in the parentheses beside Entry but it didn't work and showed me an error saying that tk() does not support float. Any help would be appreciated!
I wrote a simple script to demonstrate how to do what you want:
from Tkinter import Tk, Button, Entry, END
root = Tk()
def click():
"""Handle button click"""
# Get the input
val = myentry.get()
try:
# Try to make it a float
val = float(val)
print val
except ValueError:
# Print this if the input cannot be made a float
print "Bad input"
# Clear the entrybox
myentry.delete(0, END)
# Made this to demonstrate
Button(text="Print input", command=click).grid()
myentry = Entry()
myentry.grid()
root.mainloop()
When you click the button, the program tries to make the text in the entrybox a float. If it can't, it prints "Bad input". Otherwise, it prints the float in the terminal.