oliver
Posts: 26
Joined: Sun Mar 30, 2014 4:53 pm

tkinter extra window

Mon Nov 03, 2014 10:31 am

Hi
In tkinter i have have written a code which creates a login page for my robot controller application but it comes up with an extra window named tk #2. I get my normal login window and then when i login it opens my robot controller application and closes the login page but the other tk #2 window is still there :( why would this be and is there a way to stop it happening as i would like a bug free software i could run over VNC on a IPad or laptop.

Code: Select all

from Tkinter import *

def login():
    if u.get() == 'p' and p.get() == 'p':
        print 'your logged in'
        root = Tk()
        root.title('Robot Controller')
        top.destroy()
        root.mainloop()
    else:
        passwordincorrect = Label(root, text='Password incorrect please try again', bg='yellow', fg='red')
        passwordincorrect.pack()


#create window
top=Toplevel()

top.title('Login')
top.configure(background='yellow')
#make the username label
username = Label(top, text='Username:', bg='yellow')
username.pack()
#make the username entry widget
u = Entry(top)
u.pack()
#make the password label
password = Label(top, text='Password:', bg='yellow')
password.pack()
#make the password entry widget
p = Entry(top)
p.pack()
#make the login button
login = Button(top, text='Login:', command=login, fg='white', bg='red')
login.pack()

top.mainloop()



thanks in advance,
Olly :P

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter extra window

Mon Nov 03, 2014 1:10 pm

Single line of code fix

Code: Select all

from Tkinter import *

def login():
    if u.get() == 'p' and p.get() == 'p':
        print 'your logged in'
        root = Tk()
        root.title('Robot Controller')
        top.destroy()
        root.mainloop()
    else:
        passwordincorrect = Label(root, text='Password incorrect please try again', bg='yellow', fg='red')
        passwordincorrect.pack()


#create window
top=Tk()

top.title('Login')
top.configure(background='yellow')
#make the username label
username = Label(top, text='Username:', bg='yellow')
username.pack()
#make the username entry widget
u = Entry(top)
u.pack()
#make the password label
password = Label(top, text='Password:', bg='yellow')
password.pack()
#make the password entry widget
p = Entry(top)
p.pack()
#make the login button
login = Button(top, text='Login:', command=login, fg='white', bg='red')
login.pack()

top.mainloop()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter extra window

Mon Nov 03, 2014 1:25 pm

An additional fix to make your code for the incorrect password work

Code: Select all

from Tkinter import *
import tkMessageBox

def login():
    if u.get() == 'p' and p.get() == 'p':
        print 'your logged in'
        root = Tk()
        root.title('Robot Controller')
        top.destroy()
        root.mainloop()
    else:
        tkMessageBox.showwarning("Incorrect Username or Password","You have typed something incorrectly")

#create window
top=Tk()

top.title('Login')
top.configure(background='yellow')
#make the username label
username = Label(top, text='Username:', bg='yellow')
username.pack()
#make the username entry widget
u = Entry(top)
u.pack()
#make the password label
password = Label(top, text='Password:', bg='yellow')
password.pack()
#make the password entry widget
p = Entry(top)
p.pack()
#make the login button
login = Button(top, text='Login:', command=login, fg='white', bg='red')
login.pack()

top.mainloop()


Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
RaTTuS
Posts: 10563
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: tkinter extra window

Mon Nov 03, 2014 1:53 pm

s/your/you're/
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: tkinter extra window

Mon Nov 03, 2014 3:13 pm

RaTTuS wrote:s/your/you're/
:shock:

oliver
Posts: 26
Joined: Sun Mar 30, 2014 4:53 pm

Re: tkinter extra window

Tue Nov 04, 2014 5:32 pm

thanks, would never have worked out how to do that myself and also the incorrect password thing, i always get the password right so i would never have known :D :D :D

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter extra window

Wed Nov 05, 2014 1:14 pm

You are still learning.

It is always good practice to test all all the different decisions in your code and consider what should happen if a user does something incorrectly.

Good luck with your project.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”