Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:tkinter-askyesno方法打开一个空窗口_Python_User Interface_Dialog_Tkinter - Fatal编程技术网

Python:tkinter-askyesno方法打开一个空窗口

Python:tkinter-askyesno方法打开一个空窗口,python,user-interface,dialog,tkinter,Python,User Interface,Dialog,Tkinter,我使用此选项从用户处获取是/否,但它会打开一个空窗口: from Tkinter import * from tkMessageBox import * if askyesno('Verify', 'Really quit?'): print "ok" 这扇空窗户不会消失。我怎样才能防止这种情况 这行不通: Tk().withdraw() showinfo('OK', 'Select month') print "line 677" root = Tk

我使用此选项从用户处获取是/否,但它会打开一个空窗口:

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

这扇空窗户不会消失。我怎样才能防止这种情况

这行不通:

    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])] 

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

from Tkinter import *
from tkMessageBox import *
Tk().withdraw()
askyesno('Verify', 'Really quit?')
这不是一个漂亮的解决方案,但它确实有效


更新

不要创建第二个Tk窗口

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")
...

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

from Tkinter import *
from tkMessageBox import *
Tk().withdraw()
askyesno('Verify', 'Really quit?')
这不是一个漂亮的解决方案,但它确实有效


更新

不要创建第二个Tk窗口

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")
...

Tkinter要求在创建任何其他小部件、窗口或对话框之前存在根窗口。如果在创建根窗口之前尝试创建对话框,tkinter将自动为您创建根窗口

解决方案是显式创建一个根窗口,如果不希望它可见,则将其撤消


您应该始终只创建一个
Tk
实例,并且您的程序应该设计为在该窗口被破坏时退出

Tkinter要求在创建任何其他小部件、窗口或对话框之前存在根窗口。如果在创建根窗口之前尝试创建对话框,tkinter将自动为您创建根窗口

解决方案是显式创建一个根窗口,如果不希望它可见,则将其撤消


您应该始终只创建一个
Tk
实例,并且您的程序应该设计为在该窗口被破坏时退出

但是我在这个对话框之后使用了其他对话框,由于某种原因,您的解决方案停止了我的程序。知道为什么吗?Tk().draw()showinfo('OK','Please choose')root=Tk()root.title(“报告月”)months=[“一月”、“二月”、“三月”]def get_sel():sel_list.append(Lb1.curselection())root.destroy()def cancel():root.destroy()B=按钮(root,text=“OK”,command=get_sel)C=按钮(root,text=“Cancel”,command=Cancel)Lb1=Listbox(root,selectmode=SINGLE)用于枚举(月份)中的i,j:Lb1.insert(i,j)Lb1.pack()B.pack()C.pack()打印“第702行”root.mainloop()返回月份[int(sel_list[0][0])]你能在你的问题主体中贴出来吗?很难看出到底发生了什么。@falsetru nice!发生了什么事?@alwbtc,读一下Bryan Oakley的解释。但是我在这个对话框之后使用了其他对话框,出于某种原因,你的解决方案停止了我的程序。知道为什么吗?Tk().draw()showinfo('OK','Please choose')root=Tk()root.title(“报告月份”)月份=[“一月”、“二月”、“三月”]定义获取选择():选择列表。追加(Lb1.curselection())根目录。销毁()定义取消():根目录。销毁()B=按钮(根目录,text=“OK”,command=get\u选择)C=按钮(根目录,text=“取消”,command=cancel)Lb1=列表框(根目录,selectmode=SINGLE)对于枚举中的i,j(月):Lb1.insert(i,j)Lb1.pack()B.pack()C.pack()打印“第702行”root.mainloop()返回月[int(sel_list[0][0])]你能在你的问题主体中贴出来吗?很难看出到底发生了什么。@falsetru nice!发生了什么事?@alwbtc,阅读布莱恩·奥克利的解释。