简体   繁体   English

Python 3和tkinter通过单击按钮打开新窗口

[英]Python 3 and tkinter opening new window by clicking the button

当用户单击Tkinter和Python 3中的按钮时,如何打开新窗口?

You can open a new window by creating a new instance of the Tkinter class Toplevel . 您可以通过创建Tkinter类Toplevel的新实例来打开一个新窗口。

For example: 例如:

import Tkinter as tk

class View(tk.Frame):
    count = 0
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        b = tk.Button(self, text="Open new window", command=self.new_window)
        b.pack(side="top")

    def new_window(self):
        self.count += 1
        id = "New window #%s" % self.count
        window = tk.Toplevel(self)
        label = tk.Label(window, text=id)
        label.pack(side="top", fill="both", padx=10, pady=10)

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

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

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