Python tkinter点击事件未处理

时间:2018-01-03 14:08:22

标签: python search tkinter click

我在下面编写了Python tkinter脚本。 由于某种原因,点击事件没有做出反应:当我选择其他条目时,我希望无线电按钮切换,但不执行点击功能。我不明白为什么不。

我认为绑定没问题?

任何看到我做错的人?

所有细节都在代码中

import tkinter as tk
import tkinter.ttk as ttk

class PyExplore(ttk.Frame):
    def __init__(self, master = None, dataframes = None):
        # Construct the Frame object.
        ttk.Frame.__init__(self, master)
        # Bind click event
        self.bind('<Button-1>', self.click)
        # Place the main frame on the grid
        self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
        # Store dataframes
        self.dataframes = dataframes
        # Create widgets
        self.createWidgets()

    # Callback function - Clicked somewhere
    def click(self, event):
        print(self.focus_get())

        if self.focus_get() == self.entryY:
            self.selectedVar.set('Y')
        elif self.focus_get() == self.entryX:
            self.selectedVar.set('X')

    def createWidgets(self):  
        # Get top window 
        self.top = self.winfo_toplevel()

        # Make it stretchable         
        self.top.rowconfigure(0, weight=1)
        self.top.columnconfigure(0, weight=1)

        # Make certain rows and columns stretchable
        self.rowconfigure(0, weight=1)        
        self.columnconfigure(0, weight=1)

        # Create frameSelection
        self.frameSelection = ttk.Frame(self, relief='ridge', borderwidth=4)
        # Make frameSelection stretchable
        self.frameSelection.rowconfigure(4, weight=1)
        self.frameSelection.columnconfigure(1, weight=1)

        # Y, X labels
        self.selectedVar = tk.StringVar()
        self.selectedVar.set('Y')
        self.radiobuttonY = ttk.Radiobutton(self.frameSelection, text='Y       ', variable=self.selectedVar, value='Y')
        self.radiobuttonY.grid(row=0, column=0, sticky = tk.NE + tk.SW, padx =1, pady=1)
        self.radiobuttonX = ttk.Radiobutton(self.frameSelection, text='X       ', variable=self.selectedVar, value='X')
        self.radiobuttonX.grid(row=1, column=0, sticky = tk.NE + tk.SW, padx =1, pady=1)


        # Y, X entries
        self.entryY = ttk.Entry(self.frameSelection)
        self.entryY.grid(row=0, column=1, sticky = tk.NE + tk.SW, padx =1, pady=1)
        self.entryX = ttk.Entry(self.frameSelection)
        self.entryX.grid(row=1, column=1, sticky = tk.NE + tk.SW, padx =1, pady=1)


        self.frameSelection.grid(row=0, column=0, sticky=tk.NW+tk.SE)

# Allow the class to run stand-alone.
if __name__ == "__main__":
    PyExplore().mainloop()

1 个答案:

答案 0 :(得分:0)

替换:

self.bind('<Button-1>', self.click)

使用:

self.bind_all('<Button-1>', self.click)

点击窗口小部件的子窗口并不一定意味着父窗口小部件的焦点太明显了。