Reputation: 463
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()
Upvotes: 1
Views: 98
Reputation: 9622
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
.
Upvotes: 3