Python tkinter点击事件未处理

问题描述 投票:-1回答:1

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

我认为绑定没问题?

谁看到我做错了什么?

所有细节都在代码中

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()
python search tkinter click
1个回答
0
投票

更换:

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

有:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.