Home > front end >  Label widget created in function does not show in GUI
Label widget created in function does not show in GUI

Time:07-27

I have a register form consisting of two entries (password, confirm_password), a label for each entry, a button and a checkbox. I packed the creation of the widgets inside a register() function.

Now my Labels wont be represented on the GUI (entries, button and checkbox are also created inside the register() function) after packing all the register form related widgets inside said function (before that worked)

Here's a snippet of my function:

root = Tk()
root.title(WINDOW_TITLE)
root.geometry("400x400")
root.minsize(400, 400)
root.maxsize(400, 400)
mainframe = ttk.Frame(root, padding=50)
mainframe.grid()

def register():
   # Create master password label
   create_master_label_text = StringVar()
   create_master_label_text.set(CREATE_MASTER_TEXT)
   create_master_label = ttk.Label(mainframe, textvariable=create_master_label_text)
   create_master_label.grid(column=0, row=0, pady=5)

   # Create master password entry
   create_master_entry_text = StringVar()
   create_master_entry = ttk.Entry(mainframe, width=50, textvariable=create_master_entry_text, show='*')
   create_master_entry.grid(column=0, row=1, pady=(0, 15))

   # Create master password CONFIRM label
   create_master_label_confirm_text = StringVar()
   create_master_label_confirm_text.set(CONFIRM_MASTER_TEXT)
   create_master_confirm_label = ttk.Label(mainframe, textvariable=create_master_label_confirm_text)
   create_master_confirm_label.grid(column=0, row=3, pady=5)

   # Create master password CONFIRM entry
   create_master_entry_confirm_text = StringVar()
   create_master_entry_confirm = ttk.Entry(mainframe, width=50, textvariable=create_master_entry_confirm_text, show='*')
   create_master_entry_confirm.grid(column=0, row=4)

   # Creater master password button
   create_master_button = ttk.Button(mainframe, text="Create Password", command=lambda: createMaster(create_master_entry_text.get(), create_master_entry_confirm_text.get()))
   create_master_button.grid(column=0, row=5, pady=(20, 0))

   cb_val = IntVar()
   cb_val.set(0)
   password_checkbox = ttk.Checkbutton(mainframe, variable=cb_val, text='Show password', command=lambda: ToggleShowPassword(cb_val.get(), create_master_entry, create_master_entry_confirm))
   password_checkbox.grid(column=0, row=6, pady=(20,0))
   
if firstUse == True:
   register()


root.mainloop()

If I move the labels out of the function, everything works again.

Thanks in advance

Pic of the GUI

CodePudding user response:

because you defined the textvariable of the labels inside the register function, as soon as the code exits the function the variables are deleted. As I see it you have a number of options to solve this.

  1. you can declare the variables as global variables with global command
  2. you can declare the variable outside the function
  3. you can use the labels without textvariable in the following way:

declare label: label =ttk.Label(master, text="initial text")

change text: label["text"] = "new text"

  • Related