简体   繁体   English

由于类结构,Tkinter 返回键绑定不起作用

[英]Tkinter Return key bind doesn't work due to class structure

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()

Simple Button bind I had intended to work as is as follows:我打算按如下方式工作的简单按钮绑定:

from tkinter import *
master = Tk()

def callback(event=None):
    print("Hello " + entry.get())

entry = StringVar()
e = Entry(master, textvariable = entry, width = 15)
e.pack()

b = Button(master, text="OK", command = callback)
b.pack()
master.bind("<Return>", callback)

mainloop()

I would just like to have a simple button bind like the one above but I can't seem to find a way for my main program to work.我只想有一个像上面那样的简单按钮绑定,但我似乎找不到让我的主程序工作的方法。 I think it's due to the way I structured my app, but I'm not entirely sure.我认为这是由于我构建应用程序的方式,但我不完全确定。

On your sample you bind to the window itself.在您的样本上,您绑定到窗口本身。 You can do so in the other one as well in a number of ways:您也可以通过多种方式在另一个中执行此操作:

#1 bind to page object's direct parent, which happens to be a Toplevel-like
#self.master.bind('<Return>', self.bindHello)

#2 recursively search page object's parents, and bind when it's a Toplevel-like
#self.winfo_toplevel().bind('<Return>', self.bindHello)

#3 bind to page object's inner frame's parent's parent, which happens to be a Toplevel-like
#frame.master.master.bind('<Return>', self.bindHello)

#4 recursively search page object's inner frame's parents, and bind when it's a Toplevel-like
frame.winfo_toplevel().bind('<Return>', self.bindHello)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM