簡體   English   中英

由於焦點集中在前一幀上,因此Tkinter綁定方法不起作用

[英]Tkinter bind method not working because of focus in entry on previous frame

我有一個由多個框架組成的Tkinter GUI。

在第1幀中,有3個條目和1個按鈕。 這些條目將字符串值設置為某些變量,並且該按鈕使第2幀出現。 在第2幀中有1個標簽和1個按鈕。 該按鈕關閉程序。

在第2幀中,我有2種綁定方法。 我需要在按下Mouse-Button-1(左鍵單擊)時啟動一個功能,而在按下Escape鍵時啟動另一個功能。

問題是,如果我填寫了條目(在第1幀中),則按Escape鍵不起作用,而Mouse-Button-1起作用了(在第2幀中)。 如果我不填寫條目,一切正常。

我知道這是關注的問題。 當我填充條目時,它們會獲得焦點,但是如果我不填充它們,則當第2幀出現時,它將獲得焦點,並且Escape鍵可以正常工作。

self.focus_set() 2幀中的self.focus_set()方法使Mouse-Button-1起作用:實際上,如果我不使用此方法,那么即使此鍵綁定也不起作用。

這是我的代碼:

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

看到就是這么簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM