我正在创建一个用户管理系统,允许管理员创建用户/员工帐户。当使用默认的 tkinter“Entry”小部件时,它放置正确。 correct placement
但是,当我使用我的 tk 条目版本 (LabeledEntry) 时,它的位置类似于 this
这是我更改的条目小部件的代码:
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
传递给父类(super class) ctor,这可能是问题的一部分:
class LabeledEntry(tk.Entry):
def __init__(self, master=None, label = ""):
super().__init__(master=master)
# ...
关于python - 为什么我修改后的 tkinter 小部件无法正确放置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60225382/