دنبال کننده ها

۱۳۹۶ اسفند ۱۰, پنجشنبه

python - Tkinter Return key bind doesn't work due to class structure

[ad_1]



I understand how to bind keys when it's just on a simple frame but since I built my app in a different way, I can't seem to figure out how to bind the return key to press the button or run the function that the button is bounded to. I've been searching for a similar question by others on the website but I haven't found one similar to mine.



I've toned down the rest of my code and have it below:



import tkinter as tk
from tkinter import *

class POS(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self, *args, **kwargs)

container = tk.Frame(self)
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 (ErrorPage, MainPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column = 0, sticky = "nsew")

self.show_frame(MainPage)

def show_frame(self,cont):
frame = self.frames


I understand how to bind keys when it's just on a simple frame but since I built my app in a different way, I can't seem to figure out how to bind the return key to press the button or run the function that the button is bounded to. I've been searching for a similar question by others on the website but I haven't found one similar to mine.



I've toned down the rest of my code and have it below:



import tkinter as tk
from tkinter import *

class POS(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self, *args, **kwargs)

container = tk.Frame(self)
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 (ErrorPage, MainPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column = 0, sticky = "nsew")

self.show_frame(MainPage)

def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()


class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
frame = tk.Frame(self)
frame.pack(fill = BOTH)

button = Button(frame, text = "OK", command = self.bindHello)
button.pack(pady=5, padx=10)

frame.bind("<Return>", self.bindHello)
self.bind("<Return>", self.bindHello)

def bindHello(self, event=None):
print("HELLO1")


#Yes this doesn't do anything but I need it for the frame container as set before
class ErrorPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
frame = tk.Frame(self)
frame.pack(fill = BOTH)

button = Button(frame, text = "OK", command = self.bindHello)
button.pack(pady=5, padx=10)

frame.bind("<Return>", self.bindHello)

def bindHello(self, event=None):
print("HELLO2")


app = POS()
app.mainloop()


frame.tkraise()


class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
frame = tk.Frame(self)
frame.pack(fill = BOTH)

button = Button(frame, text = "OK", command = self.bindHello)
button.pack(pady=5, padx=10)

frame.bind("<Return>", self.bindHello)
self.bind("<Return>", self.bindHello)

def bindHello(self, event=None):
print("HELLO1")


#Yes this doesn't do anything but I need it for the frame container as set before
class ErrorPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
frame = tk.Frame(self)
frame.pack(fill = BOTH)

button = Button(frame, text = "OK", command = self.bindHello)
button.pack(pady=5, padx=10)

frame.bind("<Return>", self.bindHello)

def bindHello(self, event=None):
print("HELLO2")


app = POS()
app.mainloop()



[ad_2]

لینک منبع