user7541121
user7541121

Reputation:

Dialog box Placement in Tkinter with Python

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

Upvotes: 0

Views: 391

Answers (1)

CommonSense
CommonSense

Reputation: 4482

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)

Upvotes: 2

Related Questions