在 tkinter 中捕获框架入口 [英] Trapping Frame Entrance in tkinter

查看:22
本文介绍了在 tkinter 中捕获框架入口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绑定到框架的入口/出口.这很容易,但似乎当我将鼠标悬停在 Frame 中的某个小部件上时,它会被困为 Frame 的出口(我想是因为 Frame 在小部件区域中不可见".你可以看到这个通过运行以下代码来实现效果,该代码旨在显示文本字段(标签),然后在输入框架时更改为可编辑字段.一旦按钮位于文本区域本身,它就会回到标签.我是很难想出解决这个问题的方法.(我需要设置框架的入口,而不仅仅是文本区域.这只是一个玩具示例)

I'd like to bind to entrance/exit to/from a Frame. That's pretty easy, but it seems that when I mouse over some widget that is in the Frame, that's trapped as an exit from the Frame (I suppose because the Frame is not "visible" in the area of the widget. You can see this effect by running the following code, which is meant to display a text field (Label) and then change to an editable field when the Frame is entered. Once the button is in the text area itself, it's back to a Label. I'm having trouble of conceiving a way around this. (I need to trap entrance to the Frame, not just the text area. This is just a toy example)

from tkinter import *
class MainWindow(Frame):
    def __init__(self,master):
        super().__init__(master)
        self.master = master
        #self.master.state('zoomed')
        self.pack()
        self.edit = 0
        self.initUI()

    def initUI(self):
        self.c = Canvas(self, height = 100, width = 400, bg = "red")
        self.c.pack()
        self.bind('<Enter>', lambda *args: textarea.display(1))
        self.bind('<Leave>', lambda *args: textarea.display(0))
        self.textstring = StringVar()
        self.textstring.set("Hello")
        textarea = TextArea(self.c,self.edit,self.textstring)
        textarea.display(2)
        self.c.create_window(10,10,window = textarea,anchor = NW)

class TextArea(Frame):
    def __init__(self,master,active,textstr):
        super().__init__()
        self.master = master
        self.active = active
        self.textstr = textstr
        self.inflag = False

    def display(self,e):
        if e == 0:
            for child in self.winfo_children():
                child.destroy()
            L = Label(self,text = self.textstr.get(),relief = "flat")
            L.pack()
        elif e ==1:
            for child in self.winfo_children():
                child.destroy()
            E = Entry(self,textvariable = self.textstr,width = 10)
            E.pack()
        elif e == 2:
            L = Label(self, text=self.textstr.get(), relief="flat")
            L.pack()

root = Tk()
mainframe = MainWindow(root)
mainframe.pack()
root.mainloop()   

推荐答案

这里的实际问题相当微妙:在 TextArea__init__ 中,您未能通过master 参数给超类,所以它默认为根窗口的子窗口.换句话说,TextArea 实际上是 MainWindow 的兄弟,而不是您想要的后代.因此,必须有一个 从一个开始,以便 另一个.解决办法是做super().__init__(master),就像你在MainWindow中所做的一样.

The actual problem here is rather subtle: in TextArea's __init__, you failed to pass on the master parameter to the superclass, so it defaulted to being a child of the root window. In other words, the TextArea is actually a sibling of the MainWindow, rather than a descendant as you intended. It is thus necessary that there be a <Leave> from one in order to <Enter> the other. The solution is to do super().__init__(master), just like you did in MainWindow.

这篇关于在 tkinter 中捕获框架入口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆