![]() ![]() Yes from Tkinter import Tk, Button, Frame You should try to be explicit whenever you import in Python. Miscellaneous tips Avoid using wildcard imports (See How do I make Bind and Command do the same thing in tkinter?) Master.bind('f', lambda event: self.func()) self.button = Button(frame, text="Hello", command=self.func) If the event parameter isn't being used, you can discard it by binding your key to a lambda function. ![]() Self.button = Button(frame, text="Hello", command=self.func) self.button = Button(frame, text="Hello", command=self.func)įull Example: # from tkinter import Button, Frame, Tk # Python 3įrom Tkinter import Button, Frame, Tk # Python 2 Since the parameter is unused, you can prefix it with an underscore ( _event) to prevent warnings. Method 1: Use an optional event parameterĪs you have already discovered yourself, you can use an optional event parameter that you don't depend on. There are two ways to share a function between a bind keypress a command button. How to bind a keyboard key to a tkinter button TL DR: Yes, binding the keyboard key and button to the same function is a proper solution. The intention of the project is to make a simple image viewer in python, with buttons in the GUI for basic navigation functions, and keys mapped to those same functions. _image(0,0, image=self.display_image, anchor="nw") # Make a tkinter canvas, plug in the image, and pack the canvas. Self.display_image = ImageTk.PhotoImage(self.image) Self.image = Image.open(self.imgfilepath) ![]() Self.slogan = Button(frame, text="Hello", command = self.f) Self.button = Button(frame, text="QUIT", fg="red", command=quit) # Create and load two buttons in to the above-created frame. # Create and load a frame into the tk (tkinter) window. Imgfilepath = os.path.join(imgpath, imgfile) #imgfilepath = os.path.join(self.imgpath, self.imgfile) Imgpath = '/Users/user/Dropbox/Camera Uploads' Is this the proper (pythonic) way to bind a keyboard key and GUI button? # start.py Originally I planned to bind the keystroke to the GUI button itself, but that seemed impossible, and potentially unwise after I considered it later. I'm specifically asking about binding a keyboard keystroke to a button in a GUI. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |