Почему мой измененный виджет tkinter не помещается правильно? - PullRequest
1 голос
/ 14 февраля 2020

Я создаю систему управления пользователями, которая позволяет администратору создавать учетные записи пользователей / сотрудников. При использовании стандартного виджета tkinter «Entry» он размещается правильно. правильное размещение

Однако, когда я использую свою версию записи 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.

Есть ли способ решить эту проблему?

1 Ответ

3 голосов
/ 14 февраля 2020

Вы не передаете master в ctor суперкласса, что, вероятно, является частью проблемы:

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