0

I'm making a tkinter gui with python,but its not proper,When i click on Activate it should open a new box prompting user for Username and Pass,but there is some error;I have defined the problem below
Here is the code I am using:

import Tkinter
import tkMessageBox
from ttk import *
from Tkinter import *

root = Tk()
top = Tk()

def helloCallBack():

top.title("Activation")

Label(top, text="Username").grid(row=0, sticky=W, padx=4)
Entry(top).grid(row=0, column=1, sticky=E, pady=4)

Label(top, text="Pass").grid(row=1, sticky=W, padx=4)
Entry(top).grid(row=1, column=1, sticky=E, pady=4)

Button(top, text="Submit").grid(row=2, column=1)

B = Tkinter.Button(text ="Activate", command = helloCallBack)

B.pack()
root.mainloop()
top.mainloop()

So the output that I'm getting is ;

And when i click on activate: pic2

Two problems here
1.There is a blank box behind the root box when i run the program,how do i get rid of that?
2.The first message box(root) does not get deleted when i click on activate

1 Answer 1

2

Your main mistake is two mainloops in your code (You trying to run two separate programms). Use Toplevel() widget instead of new instance of Tk() for your new box with username/pass pair and destroy method to close it.

So here's example:

from Tkinter import *


def show_form():
    root = Tk()
    b = Button(text="Activate", command=lambda: show_call_back(root))
    b.pack()
    root.mainloop()


def show_call_back(parent):
    top = Toplevel(parent)

    top.title("Activation")
    Label(top, text="Username").grid(row=0, sticky=W, padx=4)
    Entry(top).grid(row=0, column=1, sticky=E, pady=4)
    Label(top, text="Pass").grid(row=1, sticky=W, padx=4)
    Entry(top).grid(row=1, column=1, sticky=E, pady=4)
    Button(top, text="Submit", command=top.destroy).grid(row=2, column=1)

show_form()

In addition, this site is very recommened for you!

And some links:

Toplevel widget

Entry widget (and how to grab strings from it and I think this is your next step)

Sign up to request clarification or add additional context in comments.

2 Comments

@Lafexlos, I'm agree with you, thank you for addition! This is true and there's even more recommendations to OP like using class-like structure (smth like building your own class App(tk.Tk)) with methods and attributes to store this variables. But I assume that OP is new to python/tkinter and this info would be to complicated for him(or too straight so here's no room to own research, just copy/past). I just grabbed OP's code and made with it what he wanted.
Thank you ;) , yes i'm new to Python with Tkinter,so in the process of learning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.