0

I have a few issues while creating windows with Tkinter on Python 3.10 I've already created the main window which consists of a login, password and username with a "Login" and a "register" button. I have associated functions for that buttons like a "Create a new window" function so the user can register himself or just to log in the application. But a few things happened and I really don't know why.

Here is my code:

from tkinter import *
import os

master = Tk()
master.title("Boa forma")
master.geometry("490x560+610+153")
master.iconbitmap("icone.ico")
master.resizable(False, False)

#global variable
esconda_senha = StringVar()

#functions

def nova_janela():
    master1 = Tk()
    master1.title("Nova janela")
    master1.geometry("490x550+400+153")
    master.destroy()

def nova_janela_cadastro():
    master2 = Tk()
    master2.title("Cadastro")
    master2.geometry("490x550+400+153")

#import images

img_fundo = PhotoImage(file="Títulonovo.png")
img_botao_cadastro = PhotoImage(file="Entrarnovo.png")
img_botao = PhotoImage(file ="Entrar.png")
img_fundo_cadastro = PhotoImage(file="Registrar.png")

#create label
lab_fundo = Label(master, image=img_fundo)
lab_fundo.pack()

'''lab_fundo_cadastro = Label(master2, image=img_fundo_cadastro)'''

#creating the box so the user can entry ihs data
en_id = Entry(master, bd=2, font=("Calibri", 15), justify = CENTER)
en_id.pack()
en_id.place(width=349, height = 60, x=70, y=113)

en_email = Entry(master, bd =2, font=("Calibri", 15), justify = CENTER)
en_email.pack()
en_email.place(width = 350, height = 60, x = 69, y = 225)

en_senha = Entry(master, textvariable = esconda_senha, show="*", bd =2, font=("Calibri", 15), justify = CENTER)
en_senha.pack()
en_senha.place(width = 350, height = 60, x = 70, y = 330)

#Register window
'''
ca_id_ouSenha = Entry(master2, bd=2, font=("Calibri",15), justify = CENTER)
ca_id_ouSenha.pack()
ca_id_ouSenha.place(width = 349, height = 0, x= 70, y=113)
'''

#buttons

bt_entrar=Button(master, bd =0, image= img_botao, command = nova_janela)
bt_entrar.place(width = 118, height = 64,x=110, y=454)

bt_cadastro=Button(master, bd=0,  image = img_botao_cadastro, command=nova_janela_cadastro)
bt_cadastro.place(width = 118, height = 64, x = 268, y= 454)


master.mainloop()

So, as you can see I've inserted some (''') to the code in register window and '''lab_fundo_cadastro = Label(master2, image=img_fundo_cadastro)''' because if they are indeed in that code they make the whole thing go wrong (e.g. the buttons disappear from my initial window and the user can't make any progress while using the application).

So, what am I doing wrong here? Should I create classes, make a def for everything... I could use some help here.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
JoseMarchi
  • 33
  • 5

1 Answers1

0

The new window master2 is created in a function. When the function ends the name master2 will be garbage collected and not available to the rest of the program. Same goes for master1.

Also, when you are creating a new window you should use the Toplevel() widget and not Tk(). See Why are multiple instances of Tk discouraged?

As for general application structure, you might want to have a look at Best way to structure a tkinter application?

figbeam
  • 7,001
  • 2
  • 12
  • 18