I'm very new to python and I don't know why but my code keeps crashing when I implement the listener specifically at the
with Listener(on_press=press) as listener: listener.join()
and when it crashes it doesn't give me a error message
here's the entirety of my code:
import tkinter as tk
from tkinter.constants import LEFT, N, NE, NW, W
from PIL import Image, ImageTk
import tkinter.ttk as ttk
import time
from pynput.keyboard import Key, Controller
from pynput.keyboard import Listener
self = tk.Tk()
self.title('Key spammer')
self.iconbitmap("D:\Vs code repos\Key spammer\keyboard-icon.ico")
#Set the geometry of frame
self.geometry("350x550")
self.resizable(False, False)
#Button exit function
def exit_prog():
exit()
def press(key):
print(key)
#press interval
self.grid_rowconfigure(20, weight=1)
self.grid_columnconfigure(20, weight=1)
labelframe = ttk.Labelframe(self,text= "Press interval")
labelframe.grid(row=1, column=0, padx= 25, sticky=N)
class Lotfi(ttk.Entry):
def __init__(self, master=None, **kwargs):
self.var = tk.StringVar()
ttk.Entry.__init__(self, master, textvariable=self.var, **kwargs)
self.old_value = ''
self.var.trace('w', self.check)
self.get, self.set = self.var.get, self.var.set
def check(self, *args):
if self.get().isdigit():
# the current value is only digits; allow this
self.old_value = self.get()
else:
# there's non-digit characters in the input; reject this
self.set(self.old_value)
#Entry interval
hoursentry = Lotfi(labelframe, width= 10)
hoursentry.grid(row=1, column=1, padx= 5, pady= 10)
hoursentry.insert(0, '0')
hourslabel = ttk.Label(labelframe, text= "hours")
hourslabel.grid(row=1, column=2)
minutesentry = Lotfi(labelframe, width= 10)
minutesentry.grid(row=1, column=3)
minutesentry.insert(0, '0')
minuteslabel = ttk.Label(labelframe, text= "mins")
minuteslabel.grid(row=1, column=4)
secondsentry = Lotfi(labelframe, width= 10)
secondsentry.grid(row=1, column=5)
secondsentry.insert(0, '0')
secondeslabel = ttk.Label(labelframe, text= "secs")
secondeslabel.grid(row=1, column=6)
labelframekey = ttk.Labelframe(self,text= "Key options")
labelframekey.grid(row=2, column=0, padx= 25, pady= 25, sticky=NW)
#labelframekey.pack(side= LEFT)
keypress = ttk.Label(labelframekey, text= "Key pressed", font= 10)
keypress.grid(row=2, column=0, sticky= N)
with Listener(on_press=press) as listener:
listener.join()
class Keyreg(ttk.Entry):
def __init__(self, master=None, **kwargs):
self.var = tk.StringVar()
ttk.Entry.__init__(self, master, textvariable=self.var, **kwargs)
self.old_value = ''
self.var.trace('w', self.check)
self.get, self.set = self.var.get, self.var.set
def check(self, *args):
if press:
self.set(self.old_value)
#Keyreg = Keyreg(labelframekey, width= 10)
#keyreg.grid(row=2, column=1, sticky= N)
'''
#Creation of Option buttons
button = ttk.Button(self, text = 'Exit', command = exit_prog, width = 25)
button.grid(row=2, column=1,padx= 5, ipadx=15, ipady=10)
button2 = ttk.Button(self, text = 'Exit', command = exit_prog, width = 25)
button2.grid(row=2, column=2, ipadx=15, ipady=10)
button3 = ttk.Button(self, text = 'Exit', command = exit_prog, width = 25)
button3.grid(row=3, column=1, ipadx=15, ipady=10)
button4 = ttk.Button(self, text = 'Exit', command = exit_prog, width = 25)
button4.grid(row=3, column=2, ipadx=15, ipady=10)
'''
#Top menu for keybinds
tk.mainloop()
I tried to figure out the problem myself but when i imported it to another tab and used multiple different methods it still never crashed on those test
Please help me
CodePudding user response:
You didn't show full error message so I don't know what is your real problem but I see one problem.
Code
with Listener(on_press=press) as listener:
listener.join()
runs code which waits for the end of listener
and it stops rest of code
if you want to run it at the same time as tkinter
then you should run as
with Listener(on_press=press) as listener:
tk.mainloop()
listener.join()
or more similar to threading
(which is used by listener
)
listener = Listener(on_press=press)
listener.start()
tk.mainloop()
listener.join()