python - 为什么我修改后的 tkinter 小部件无法正确放置?

标签 python python-3.x tkinter tkinter-entry

我正在创建一个用户管理系统,允许管理员创建用户/员工帐户。当使用默认的 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/

相关文章:

python - 在掩码数组中查找出现频率最高的元素

python - 使用 matplotlib.pyplot 时如何显示节点标签?

python - 在 ipython 控制台中调试魔术功能

python - 如何在 tkinter 中更新文本标签?

python - 将项目添加到 ttk.Treeview 时出现“不匹配的左括号”错误

python - Numpy:用同一行中其他元素的最大值替换一行中的每个元素

python - Pip 在 macOS 上找不到 python 3.6 的 tensorflow

python - 关于python中的Buffer接口(interface)

python - 管理 Django 中的错误有困难 - NoReverseMatch at

python - 如何在 Tkinter 中将 Label 中的数字排列成网格?