Python:Tkinter Askyesno方法打开一个空窗口 [英] Python: tkinter askyesno method opens an empty window

查看:126
本文介绍了Python:Tkinter Askyesno方法打开一个空窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用它从用户那里获得是/否,但它打开一个空窗口:

I use this to get yes/no from user but it opens an empty window:

from Tkinter import *
from tkMessageBox import *
if askyesno('Verify', 'Really quit?'):
    print "ok"

这个空窗口不会消失。如何防止这种情况发生?

And this empty window doesnt go away. How can I prevent this?

这不起作用:

    Tk().withdraw()
    showinfo('OK', 'Select month')
    print "line 677"
    root = Tk()
    root.title("Report month")
    months = ["Jan","Feb","Mar"]
    sel_list = []
    print "line 682"

    def get_sel():
        sel_list.append(Lb1.curselection())
        root.destroy()

    def cancel():
        root.destroy()

    B = Button(root, text ="OK", command = get_sel)
    C = Button(root, text ="Cancel", command = cancel)
    Lb1 = Listbox(root, selectmode=SINGLE)

    for i,j in enumerate(months):
        Lb1.insert(i,j)


    Lb1.pack()
    B.pack()
    C.pack()
    print "line 702"
    root.mainloop()

    for i in sel_list[0]:
        print months[int(i)]
    return months[int(sel_list[0][0])] 


解决方案

显式创建根窗口,然后退出。

Create root window explicitly, then withdraw.

from Tkinter import *
from tkMessageBox import *
Tk().withdraw()
askyesno('Verify', 'Really quit?')

不是很漂亮的解决方案,但是可以。

Not beautiful solution, but it works.

更新

不要创建第二个Tk窗口。

Do not create the second Tk window.

from Tkinter import *
from tkMessageBox import *

root = Tk()
root.withdraw()
showinfo('OK', 'Please choose')
root.deiconify()

# Do not create another Tk window. reuse root.

root.title("Report month")
...

这篇关于Python:Tkinter Askyesno方法打开一个空窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆