Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.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 我无法在OSX上使用tkinter创建第二个窗口_Python_Macos_Python 3.x_Tkinter - Fatal编程技术网

Python 我无法在OSX上使用tkinter创建第二个窗口

Python 我无法在OSX上使用tkinter创建第二个窗口,python,macos,python-3.x,tkinter,Python,Macos,Python 3.x,Tkinter,当使用Python3.6和tkinter创建第二个窗口时,它不负责。我正在使用OSX10.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()

当使用Python3.6和tkinter创建第二个窗口时,它不负责。我正在使用OSX10.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()

在程序中多次使用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()

这个代码对我有用

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

我认为你处理这个问题的方法是错误的。tkinter GUI应该只有1个主循环。你的win2类什么都没做。我建议找到一个简单的tkinter示例,并从中着手。此外,PyQt是GUI的另一个选项,我想为我的应用程序创建设置窗口。这里是我的完整代码的链接,这是一个“模式窗口”。我编辑了我的答案,告诉你怎么做。我建议您也按照我的指导进行子类化,而不是创建self.root之类的属性。@МаааПааПаааааааааа。这不是我想要的。。。