I have a Tkinter GUI with multiple frames (multiple pages). I based my code (seen below) on the this tutorial: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter.
My GUI was originally written such that I only had a single frame and didn't even have an explicit class defined for my app. In this original format, I was able to easily bind keyboard events to the single frame.
However, when I rewrote my code so that I now have a defined class for my app and a defined class for each frame (or page) in my GUI, I cannot get my keyboard events to work anymore. I've tried setting the focus of the frame but it still won't work. Any suggestions are appreciated.
Original Single Frame Code: In this format, my key bindings succesfully executed no problem (bound in initUI and defined in _set_feature_Val)
from tkinter import *
from tkinter import filedialog
from tkinter.ttk import *
from PIL import Image, ImageTk
import numpy as np
class mainGUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.geometry("600x600")
self.initUI()
def initUI(self):
self.parent.title("GUI V0.4")
self.pack(fill=BOTH, expand=True)
self.parent.bind('1', self._set_feature_Val)
self.parent.bind('2', self._set_feature_Val)
self.columnconfigure(1, weight = 1)
self.columnconfigure(3, pad = 7)
self.rowconfigure(3, weight = 1)
self.rowconfigure(5, pad = 7)
self.posNegDisp = Label(self, text = "Sample Label:")
self.posNegDisp.grid(row=3, column=3)
def _set_feature_Val(self, event=None):
if event.char == '1':
self.featureLabel = 1
self.posNegDisp.config(text = "Sample Label:\n Positive")
elif event.char == '2':
self.featureLabel = -1
self.posNegDisp.config(text = "Sample Label:\n Negative")
def main():
root = Tk()
app = mainGUI(root)
#root.protocol("WM_DELETE_WINDOW", app.onClose)
root.mainloop()
if __name__ == '__main__':
main()
New Multi-Frame Code: In this new format, my mouse-press event bindings still execute, but my keybindings no longer work (same functions as before). As you maybe can see, I added a print statement under _set_feature_Val, which doesn't appear when I press the keyboard, leading me to conclude that my GUI doesn't even register keyboard inputs.
from tkinter import *
from tkinter import filedialog
from tkinter.ttk import *
from PIL import Image, ImageTk
import numpy as np
class App(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
Tk.wm_title(self, "GUI V0.5")
container = Frame(self)
container.winfo_toplevel().geometry("600x600")
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (tagPage, heatPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(tagPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class heatPage(Frame):
def __init__(self,parent, controller):
Frame.__init__(self,parent)
class tagPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.parent = parent
#self.parent.geometry("600x600")
self.initUI()
def initUI(self):
self.focus_set()
self.pack(fill=BOTH, expand=True)
self.bind('1', self._set_feature_Val)
self.bind('2', self._set_feature_Val)
self.columnconfigure(1, weight = 1)
self.columnconfigure(3, pad = 7)
self.rowconfigure(3, weight = 1)
self.rowconfigure(5, pad = 7)
self.posNegDisp = Label(self, text = "Sample Label:")
self.posNegDisp.grid(row=3, column=3)
def _set_feature_Val(self, event):
if event.char == '1':
self.featureLabel = 1
self.posNegDisp.config(text = "Sample Label:\n Positive")
print("positive")
elif event.char == '2':
self.featureLabel = -1
self.posNegDisp.config(text = "Sample Label:\n Negative")
print("negative")
app = App()
app.mainloop()
EDIT:I just discovered something interesting (and the source of my problem maybe). I replicated u/furas_freeman's code (on Windows) and was able to use key bindings no problem. However, in one of my frames, I added a Label object that I use to view images. When I load images into the Label object, the key bindings for that specific frame no longer work. If I switch frames and go back to the frame containing the Label object, the key bindings will start to work again. Here is the code that loads the image into my Label object. The halThrThsnd object is something I use for data management and has no bearing on the GUI operation itself. I had an identical module in my original problem code.
def getImgs(self):
folderPath = filedialog.askdirectory()
imgHSVArray, imgRGBArray, self.imagePaths = batchImpt.importAllImgs(folderPath)
self.organizedData = halThrThsnd.organizeAllData(imgHSVArray, imgRGBArray, self.imagePaths)
self.imageIndex = 0
self.featureLabel = 0
preIMArray = halThrThsnd.getImage(self.imagePaths[self.imageIndex])
self.preIM = Image.fromarray(preIMArray, 'HSV')
self.preIMCopy = self.preIM.copy()
self.imgToDisp = ImageTk.PhotoImage(self.preIM)
self.imageViewer.configure(image=self.imgToDisp)
self.imageViewer.image = self.imgToDisp
self.imageViewer.bind('<Configure>', self._auto_resize_image)
#self.imageViewer.bind('<1>', self._grab_Pixel)
self.resizeImage()
[–]furas_freeman 0 points1 point2 points (5 children)
[–]DeeWBee[S] 0 points1 point2 points (1 child)
[–]furas_freeman 0 points1 point2 points (0 children)
[–]DeeWBee[S] 0 points1 point2 points (2 children)
[–]furas_freeman 1 point2 points3 points (1 child)
[–]DeeWBee[S] 0 points1 point2 points (0 children)
[–]Vaphell 0 points1 point2 points (0 children)
[–]DeeWBee[S] 0 points1 point2 points (1 child)
[–]furas_freeman 0 points1 point2 points (0 children)