Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Tkinter Generate/Delete Shapes with Keyboard Events
#1
Hey all,

Brand new to Tkinter. I'm trying to create random shapes with keyboard presses (keydown), and then delete them when the key is let go (keyup). I've been struggling with doing this, how should I go about it (either a high-level view or example code)? Ideally, the user will be pressing a lot of buttons in rapid succession, so I'd also like to be able to account for multiple shapes being displayed/removed at the same/close to the same time.

I've tried using pack.forget(), .after(), and a few other functions but no success. I've also tried Googling a lot and wasn't able to find anything.
Quote
#2
show code
Quote
#3
(Aug-15-2019, 11:05 AM)Larz60+ Wrote: show code

Here's a part - I'm having issues with what I commented out. I notice that this stops the terminal from continuing to read input, which I don't want at all. I want input to constantly get processed and not wait for one task to get done to move on, as the user will be pressing these keys frequently. I also am not sure how to simulate a keydown/keyup in here and would prefer that for getting the shape to appear/disappear rather than to use a regular timer for making the shape disappear.

import tkinter as tk

def enter_pressed(event):
	print("Key pressed: ENTER")
	box = c.create_rectangle(50, 50, 100, 100, fill="red")
	# root.after(3000, c.delete(box))

def space_pressed(event):
	print("Key pressed: SPACE")

def main():
	root.bind("<Return>", enter_pressed)
	root.bind("<space>", space_pressed)

	root.mainloop()

root = tk.Tk()
root.title("My app")
root.geometry('500x500')
c = tk.Canvas(root, height=500, width=500, bg="blue")
c.pack()

main()
Quote
#4
Use a lambda statement, after needs to a function
import tkinter as tk


def enter_pressed(event):
    print("Key pressed: ENTER")
    box = c.create_rectangle(50, 50, 100, 100, fill="red")
    root.after(3000, lambda: c.delete(box))
 
def space_pressed(event):
    print("Key pressed: SPACE")
 
def main():
    root.bind("<Return>", enter_pressed)
    root.bind("<space>", space_pressed)
 
    root.mainloop()

if __name__ == '__main__':
    root = tk.Tk()
    root.title("My app")
    root.geometry('500x500')
    c = tk.Canvas(root, height=500, width=500, bg="blue")
    c.pack()
    
    main()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Delete an object created from another class alan9979 4 139 Jul-13-2019, 08:48 AM
Last Post: alan9979
  [Tkinter] how to move between text entries using the keyboard? nadavrock 1 189 Jun-09-2019, 11:00 PM
Last Post: Yoriz
  [Tkinter] How to get & delete details from each input by clicking a button Vicolas 6 455 Feb-01-2019, 11:00 AM
Last Post: Vicolas
  clear all widgets at same time (not delete/remove) shift838 0 342 Dec-17-2018, 11:55 PM
Last Post: shift838
  Generate ctrl-A behaviour edgarfinchley 3 594 Aug-24-2018, 01:13 PM
Last Post: Larz60+
  How to delete text from a tkinter Text widget? Tang 1 10,763 May-20-2018, 09:26 PM
Last Post: Larz60+
  [PyQt] touch events is not generating shridhara 0 723 Apr-23-2018, 11:39 AM
Last Post: shridhara
  Launch pdf and close on delete window event ashtona 6 1,256 Mar-22-2018, 03:04 PM
Last Post: ashtona
  [Tkinter] problem with button events Lubik_ 4 3,503 Dec-01-2017, 08:47 PM
Last Post: Windspar
  How can I draw en color shapes on python? MBlastSt 2 1,837 Oct-09-2017, 01:42 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)