Python - tkinter:打开和关闭对话框窗口

时间:2014-06-11 11:37:25

标签: python user-interface python-3.x tkinter

我是Python新手,必须编写一个简单的GUI程序,为了简单起见,我选择在tkinter中这样做。

我想要的GUI应该非常类似于在Windows上安装程序时经常遇到的对话框(您想要安装的位置,您想要的模块等)。基本上当它在python3.3中运行时,我想要一个窗口出现,其中一些选项占据了大部分窗口,然后是','返回'和'取消'底部的按钮;点击“下一步”'按钮,当前窗口关闭,打开一个看起来相同的新窗口,除了它有不同的选项(或者它可能是同一个窗口,但它的内容已被破坏,我不确定哪个更好) 。我想要的粗略布局显示在this image rough layout

我已经四处寻找代码,这些代码与此类似,但未找到任何代码。我看过this answer,但它并不是我想要的。我使用this tutorial来了解我对tkinter的了解,但我无法在其中找到答案。

这是我对我想要做的简化版本的极其糟糕的尝试:当我运行代码时,它会创建一个带有两个按钮的窗口。 ' Quit'按钮工作正常;但是,当我点击“下一步”时按钮关闭窗口并根据需要打开一个新窗口,但它也会打开另一个窗口。

from tkinter import *
from tkinter import ttk

def win1():
    mainframe = ttk.Frame(root, padding = '3 3 12 12')
    mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
    mainframe.columnconfigure(0, weight = 1)
    mainframe.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)
    ttk.Button(mainframe, text = 'Quit', command = quit).grid(
        column = 1, row = 2, sticky = W)

    root.mainloop()

def quit():
    root.destroy()

def win2():
    quit()
    new = Toplevel()
    new.title('Window 2')
    new = ttk.Frame(root, padding = '3 3 12 12')
    new.grid(column = 0, row = 0, sticky = (N, W, E, S))
    new.columnconfigure(0, weight = 1)
    new.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)

root = Tk()  
win1()

这会出现以下错误消息(我不明白):

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.3/tkinter/__init__.py", line 1478, in __call__
    return self.func(*args)
  File "<stdin>", line 23, in win2
  File "/usr/lib/python3.3/tkinter/ttk.py", line 733, in __init__
    Widget.__init__(self, master, "ttk::frame", kw)
  File "/usr/lib/python3.3/tkinter/ttk.py", line 553, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "/usr/lib/python3.3/tkinter/__init__.py", line 2078, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: this isn't a Tk applicationNULL main window

除了它实际上没有做我想要的事实之外,我觉得我正在以完全错误的方式(在一个函数中定义窗口等),并且会遇到很多我想让它变得更复杂时遇到麻烦。有没有人能够以更好的方式重写我的代码,并以某种方式帮助我构建更复杂的程序,提供资源来学习我需要的程序,我想要甚至提供建议?感谢。

2 个答案:

答案 0 :(得分:4)

您的问题是在调用quit()时关闭根Tk窗口。不要那样做。关闭后,您已经卸载了Tk,它无法为您正确处理Window系统消息。相反,如果要创建一系列对话框的应用程序,请通过撤消它来隐藏根窗口,并将每个对话框创建为新的顶层,隐藏的根目录为父级。

答案 1 :(得分:2)

我修改了你的例子,我希望能够正确理解你想要实现的目标。

from tkinter import *
from tkinter import ttk

def win1():
    mainframe = ttk.Frame(root, padding = '3 3 12 12')
    mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
    mainframe.columnconfigure(0, weight = 1)
    mainframe.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)
    ttk.Button(mainframe, text = 'Quit', command = quit).grid(
        column = 1, row = 2, sticky = W)

    root.mainloop()

def quit():
    root.destroy()

def win2():
    root.withdraw()
    new = Toplevel()
    new.title('Window 2')
    mainframe = ttk.Frame(new, padding = '3 3 12 12')
    mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
    mainframe.columnconfigure(0, weight = 1)
    mainframe.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)

root = Tk()  
win1()

希望这有帮助。