简体   繁体   English

使用Python在Tkinter中放置对话框

[英]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 我正在用python制作tkinter gui,但是它不正确,当我单击``激活''时,应该打开一个新框,提示用户输入用户名和密码,但是有一些错误;我在下面定义了问题
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: 当我点击激活时: 图片2

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? 1.运行程序时,根目录框后面有一个空白框,该如何消除?
2.The first message box(root) does not get deleted when i click on activate 2.当我单击激活时,第一个消息框(根)不会被删除

Your main mistake is two mainloops in your code (You trying to run two separate programms). 您的主要错误是代码中的两个mainloop(您试图运行两个单独的程序)。 Use Toplevel() widget instead of new instance of Tk() for your new box with username/pass pair and destroy method to close it. 使用具有用户名/密码对的新框,使用Toplevel()小部件而不是Tk()的新实例,并使用destroy方法将其关闭。

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) Entry小部件 (以及如何从中获取字符串,我认为这是您的下一步)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM