未处理Python tkinter click事件

2024-10-01 04:58:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下面编写了pythontkinter脚本。 由于某些原因,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()

Tags: theselftopcolumnframetkgridrow
1条回答
网友
1楼 · 发布于 2024-10-01 04:58:36

替换:

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

使用:

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

因为单击小部件的子部件并不一定意味着父部件的焦点太明显

相关问题 更多 >