python - 由于焦点位于前一帧的条目,Tkinter 绑定(bind)方法不起作用

标签 python tkinter

我有一个由多个框架组成的 Tkinter GUI。

在第 1 帧中有 3 个条目和 1 个按钮。这些条目将字符串值设置为某些变量,并且该按钮使第 2 帧出现。 第 2 帧中有 1 个标签和 1 个按钮。该按钮关闭程序。

在第 2 帧中,我有 2 个绑定(bind)方法。我需要在按下 Mouse-Button-1(左键单击)时启动一个函数,在按下 Escape 时启动另一个函数。

问题是,如果我填写条目(在第 1 帧中),按 Escape 键不起作用,但按 Mouse-Button-1 可以(在第 2 帧中)。如果我不填写条目,一切都会正常。

我知道这是注意力不集中的问题。当我填充条目时,它们会获得焦点,但如果我不填充它们,当第 2 帧出现时,它会获得焦点,并且 Escape 键可以正常工作。

第 2 帧中的

self.focus_set() 方法使 Mouse-Button-1 起作用:事实上,如果我不使用此方法,则即使此键绑定(bind)也不起作用。

这是我的代码:

from tkinter import *

def keyf1(Event):
    print("function 1")

def keyf2(Event):
    print("function 2")

def start(Event):
    global nometopo
    global classetopo
    global sessione

    nometopo = entry_nome.get()
    classetopo = entry_classe.get()
    sessione = entry_sessione.get()

    entry_nome.delete(0, 'end')
    entry_classe.delete(0, 'end')
    entry_sessione.delete(0, 'end')


class EPMouse(Tk):

    def __init__(self, *args, **kwargs):

        Tk.__init__(self, *args, **kwargs)
        container = 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 (Frame_1, Frame_2):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(Frame_1)

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

class Frame_1(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        global entry_nome
        global entry_classe
        global entry_sessione

        label_nome=Label(self,text="Nome soggetto: ").grid(row=2, column=0, sticky=E)
        label_classe=Label(self,text="Classificato come: ").grid(row=3, column=0, sticky=E)
        label_sessione=Label(self,text="Sessione: ").grid(row=5, column=0, sticky=E)

        entry_nome = Entry(self)
        entry_nome.grid(row=2,column=1)
        entry_classe = Entry(self)
        entry_classe.grid(row=3,column=1)
        entry_sessione = Entry(self)
        entry_sessione.grid(row=5,column=1)

        button_start = Button(self, text="START!", command=lambda: [start(Event), controller.show_frame(Frame_2)]).grid(row=6, column=1, sticky=EW)

class Frame_2(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        global label

        label_header = Label(self, text="Click sx = F1 \n Esc = F2").pack()

        button_stop = Button(self, text="STOP", command=lambda: app.destroy()).pack()

        self.bind("<Button-1>", keyf1)
        self.bind("<Escape>", keyf2)
        self.focus_set()

app = EPMouse()
app.mainloop()

也许我需要将焦点从第 1 帧中的条目移开,但我不知道如何做。

最佳答案

解决您的问题非常简单。抬起时,只需将第 2 帧置于焦点即可。

在这里我可以看到你正在使用这个函数来切换帧,所以为什么不再添加一行来解决焦点问题。

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

看看就这么简单。

关于python - 由于焦点位于前一帧的条目,Tkinter 绑定(bind)方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55880213/

相关文章:

python - 属性错误: 'str' object has no attribute '_sa_instance_state' - Flask SqlAlchemy

python - PySpark 在嵌套数组中反转 StringIndexer

python - 何时应在 TensorFlow 中使用 tf.losses.add_loss()?

python - 如何垂直合并两个图像?

关闭时出现 tkinter 和线程错误的 Python 应用程序

python - 有没有办法从 Enter 键读取按键?

python - 显示方形 Tkinter.Button 的?

linux - 使用 tkinter + gobject 时“关闭窗口”按钮不起作用

Python - Tkinter RadioButton If 语句

python - 创建文本文件(如果它们尚不存在)