Python 为什么我修改过的tkinter小部件不能正确放置?
我正在创建一个用户管理系统,允许管理员创建用户/员工帐户。使用默认tkinter条目小部件时,它会正确放置 然而,当我使用我的tk entry LabeledEntry版本时,它的位置如下 这是“我的已更改条目”小部件的代码:Python 为什么我修改过的tkinter小部件不能正确放置?,python,python-3.x,tkinter,tkinter-entry,Python,Python 3.x,Tkinter,Tkinter Entry,我正在创建一个用户管理系统,允许管理员创建用户/员工帐户。使用默认tkinter条目小部件时,它会正确放置 然而,当我使用我的tk entry LabeledEntry版本时,它的位置如下 这是“我的已更改条目”小部件的代码: class LabeledEntry(tk.Entry): #creates a version of the tkEntry widget that allows for "ghost" text to be within the entry field. de
class LabeledEntry(tk.Entry): #creates a version of the tkEntry widget that allows for "ghost" text to be within the entry field.
def __init__(self, master=None, label = ""): #which instructs the user on what to enter in the field.
tk.Entry.__init__(self)
self.label = label
self.on_exit()
self.bind('<FocusOut>', self.on_exit)
self.bind('<FocusIn>', self.on_entry)
def on_entry(self, event=None):
if self.get() == self.label: #checks if the given label is present
self.delete(0, tk.END) #If the text field of the entry is the same as the label, it is deleted to allow for user input
self.configure(fg = "black") #sets the text color to black
def on_exit(self, event=None):
if not self.get(): #checks if user entered anything into the entry when clicked into it.
self.insert(0, self.label) #If they did not, sets the text to the original label
self.configure(fg = "grey") #and changes the color of the text to grey.
有办法解决此问题吗?您没有将master传递给超类ctor,这可能是问题的一部分:
class LabeledEntry(tk.Entry):
def __init__(self, master=None, label = ""):
super().__init__(master=master)
# ...
您没有将master传递给超类ctor,这可能是问题的一部分:
class LabeledEntry(tk.Entry):
def __init__(self, master=None, label = ""):
super().__init__(master=master)
# ...
超级。uuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。