首页 > 解决方案 > 为什么我更改的 tkinter 小部件没有正确放置?

问题描述

我正在创建一个用户管理系统,允许管理员创建用户/员工帐户。使用默认的 tkinter“条目”小部件时,它会正确放置。正确放置

但是,当我使用我的 tk 条目(LabeledEntry)版本时,它会像这样放置

这是我更改的条目小部件的代码:

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.

有没有办法解决这个问题?

标签: pythonpython-3.xtkintertkinter-entry

解决方案


您没有传递master给超类 ctor,这可能是问题的一部分:

class LabeledEntry(tk.Entry):
    def __init__(self, master=None, label = ""):
        super().__init__(master=master)
        # ...

推荐阅读