Reputation: 310
i'm new to tkinter and wanted to change an already existing piece of code I made into GUI. The piece of code below is a username and password system. The thing I need help with is that I can not figure out how to get a new box or to remove the widgets of the gui. Nothing is wrong with the code below but I wanted to show you as it shows you how I've coded it and how to make a new box based on this code. Btw I am in python 3.5.1 and on windows 10.
import tkinter
from tkinter import *
import tkinter.messagebox as box
import time
def dialog1():
username=entry1.get()
password = entry2.get()
if (username == 'Noel' and password == 'Music quiz'):
box.showinfo('info','You may now enter the Music quiz')
else:
box.showinfo('info','Invalid Login')
window = Tk()
window.title('Music quiz')
window.geometry("300x125")
window.wm_iconbitmap('Favicon.ico')
frame = Frame(window)
Label1 = Label(window,text = 'Username:')
Label1.pack()
entry1 = Entry()
entry1.pack()
Label2 = Label(window,text = 'Password: ')
Label2.pack()
entry2 = Entry()
entry2.pack()
Upvotes: 2
Views: 377
Reputation: 2711
Here is edited code that I think will do what you have asked. Explanations are in the code in the form of comments.
import tkinter
from tkinter import *
import tkinter.messagebox as box
import time
def dialog1():
username=entry1.get()
password = entry2.get()
if (username == 'Noel' and password == 'Music quiz'):
box.showinfo('info','You may now enter the Music quiz')
loginframe.destroy() #remove the login frame
##code to create the quiz goes here##
else:
box.showinfo('info','Invalid Login')
window = Tk()
window.title('Music quiz')
window.geometry("300x125")
window.wm_iconbitmap('Favicon.ico')
loginframe = Frame(window) #create an empty frame for login
loginframe.pack() #put the empty frame into the window
#all elements below are put into the 'loginframe' Frame
Label1 = Label(loginframe,text = 'Username:')
Label1.pack()
entry1 = Entry(loginframe)
entry1.pack()
Label2 = Label(loginframe,text = 'Password: ')
Label2.pack()
entry2 = Entry(loginframe)
entry2.pack()
donebttn = Button(loginframe, text='Done',
command=dialog1) #create a button to continue
donebttn.pack() #display that button
mainloop()
Upvotes: 1