【问题标题】:I can't create a second window using tkinter, on os x我无法在 os x 上使用 tkinter 创建第二个窗口
【发布时间】:2017-03-15 21:10:22
【问题描述】:

使用 python 3.6 和 tkinter 创建第二个窗口时,它不负责。我正在使用 os x 10.11.6。 在 Ubuntu 等其他系统中,此代码有效。

from tkinter import *

class win2:

    def __init__(self):
        self.root = Tk()
        self.root.mainloop()

class win1:

    def __init__(self):
        self.root = Tk()

        self.button = Button(self.root)
        self.button.bind('<Button-1>', self.buttonFunc)
        self.button.pack()

        self.root.mainloop()

    def buttonFunc(self, event):
        windows2 = win2()

if __name__ == "__main__":
    window1 = win1()

【问题讨论】:

  • 我认为您处理问题的方法有误。您的 tkinter GUI 应该只有 1 个主循环。你的 win2 班级没有做任何事情。我建议找到一个简单的 tkinter 示例并从那里开始工作。此外,PyQt 是 GUI 的另一种选择

标签: python macos python-3.x tkinter


【解决方案1】:

在您的程序中多次使用Tk() 是一个非常糟糕的主意。使用它来创建根窗口,然后使用Toplevel() 来创建任何其他窗口。

def buttonFunc(self, event):
    Toplevel(self.root)

也就是说,您似乎仍然在努力做某事。你能更好地描述你的最终目标是什么吗?

要制作模态窗口(弹出窗口),请使用如下代码:

try: #python3 imports
    import tkinter as tk
except ImportError: #python3 failed, try python2 imports
    import Tkinter as tk

class Main(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        lbl = tk.Label(self, text="this is the main frame")
        lbl.pack()

        btn = tk.Button(self, text='click me', command=self.open_popup)
        btn.pack()

    def open_popup(self):
        print("runs before the popup")
        Popup(self)
        print("runs after the popup closes")

class Popup(tk.Toplevel):
    """modal window requires a master"""
    def __init__(self, master, **kwargs):
        tk.Toplevel.__init__(self, master, **kwargs)

        lbl = tk.Label(self, text="this is the popup")
        lbl.pack()

        btn = tk.Button(self, text="OK", command=self.destroy)
        btn.pack()

        # The following commands keep the popup on top.
        # Remove these if you want a program with 2 responding windows.
        # These commands must be at the end of __init__
        self.transient(master) # set to be on top of the main window
        self.grab_set() # hijack all commands from the master (clicks on the main window are ignored)
        master.wait_window(self) # pause anything on the main window until this one closes

def main():
    root = tk.Tk()
    window = Main(root)
    window.pack()
    root.mainloop()

if __name__ == '__main__':
    main()

【讨论】:

  • 我想为我的应用创建设置窗口。这是我完整代码的链接codeshare.io/GqANWx
  • 这是一个“模态窗口”。我编辑了我的答案,向您展示如何做到这一点。我建议你也按照我的方法进行子类化,而不是像self.root 这样的属性。
  • @МихайлоПилипишин 请不要要求人们为您做所有事情并给他们您的完整代码。这不是 SO 的用途......
【解决方案2】:

此代码对我有用。

from tkinter import *

class win1:

    def __init__(self):
        root = Tk()
        button = Button(root)
        button.bind('<Button-1>', self.buttonFunc)
        button.pack()
        root.mainloop()

    def buttonFunc(self, event):
        window2 = win2()

class win2(win1):

    def __init__(self):
        top = Toplevel()

if __name__ == "__main__":
    window1 = win1()

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 2020-11-25
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2013-07-20
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多