Tkinter 中的多个窗口? [英] Multiple windows in Tkinter?

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

问题描述

我是编程新手,我很难找到教如何创建使用多个窗口的 GUI 的教程.例如,如果用户单击查找"按钮,则会弹出一个包含搜索结果的窗口.我该如何实现?这在 Tkinter 中可能吗?任何对来源的建议/参考将不胜感激.谢谢.

I am new to programming, and I am having trouble finding a tutorial that teaches how to create a GUI that uses multiple windows. For example, If a user clicks a "lookup" button, a window pops up with the search results. How do I accomplish this? Is this possible within Tkinter? Any suggestions/ references to sources would be greatly appreciated. Thanks.

推荐答案

要创建第一个窗口,请创建 Tk 类的实例.所有其他窗口都是 Toplevel 的实例.

To create your first window, you create an instance of the Tk class. All other windows are instances of Toplevel.

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        b1 = tk.Button(self, text="Add another window", command = self.newWindow)
        b1.pack(side="top", padx=40, pady=40)
        self.count = 0

    def newWindow(self):
        self.count += 1
        window = tk.Toplevel(self)
        label = tk.Label(window, text="This is window #%s" % self.count)
        label.pack(side="top", fill="both", expand=True, padx=40, pady=40);

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

这篇关于Tkinter 中的多个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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