-1

I am fairly new to coding and im just trying to fix my code. i have recently made some changes which caused my code to open my intended window but also one that i don't want. Just looking for some help as to how i can get rid of the blank window. Heres the code:

from tkinter import *
import tkinter as tk

root = Tk()
root.title("Login")
username = "Sam" 
password = "Sam"

#username entry
username_entry = Entry(root)

#password entry
password_entry = Entry(root, show='*')

username_entry.grid(row=1,column=2)
password_entry.grid(row=2,column=2)

myLabel1 = Label(root,text="Username")
myLabel2 = Label(root,text="Password")

myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)

def trylogin(): 
    if username == username_entry.get() and password == password_entry.get():
        print("Correct")
        createNewWindow()
    else:
        print("Wrong")

def createNewWindow():
    app = Tk()
    newWindow = tk.Toplevel(app)
    labelExample = tk.Label(newWindow, text = "New")
    buttonExample = tk.Button(newWindow, text = "New Window button")

    labelExample.pack()
    buttonExample.pack()

button = Button(root, text="check", command = trylogin) 
button.grid(row=3, column=2)


root.mainloop()
app.mainloop()
Codi.no
  • 21
  • 4
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/a/48045508/7414759) and [When to use the Toplevel Widget](http://effbot.org/tkinterbook/toplevel.htm) and [Tkinter understanding mainloop](https://stackoverflow.com/a/29158947/7414759) – stovfl Jun 17 '20 at 09:06

1 Answers1

0

Good question, and nice code so far. This will do it. Tk's toplevel caused me a lot of headaches when I started with it as well.

def createNewWindow():
    newWindow = tk.Toplevel(root)
    labelExample = tk.Label(newWindow, text = "New")
    buttonExample = tk.Button(newWindow, text = "New Window button")

    labelExample.pack()
    buttonExample.pack()
JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24