简体   繁体   English

使用 Python,如何从另一个 GUI 调用 tkinter GUI?

[英]Using Python, how do you call a tkinter GUI from another GUI?

I created a couple of GUIs using tkinter.我使用 tkinter 创建了几个 GUI。 But now I am interested in combining them into one caller GUI.但现在我有兴趣将它们组合成一个调用者 GUI。 So the caller GUI would have buttons that, when clicked, would open the other GUIs.因此,调用方 GUI 将具有按钮,单击这些按钮时会打开其他 GUI。 However, I cannot get it to work.但是,我无法让它工作。 I've done the imports correctly (I think), edited the main functions in the subGUIs, and added the command=GUI.main in my buttons.我已经正确地完成了导入(我认为),编辑了 subGUI 中的主要功能,并在我的按钮中添加了 command=GUI.main。 I get it to load but I get errors about missing files...but when I run a GUI by itself it works fine.我让它加载,但我收到有关丢失文件的错误......但是当我自己运行 GUI 时,它工作正常。

In my research, I read that there can only be one mainloop in a Tkinter program.在我的研究中,我读到 Tkinter 程序中只能有一个主循环。 Basically, I cannot use a Tkinter GUI to call another Tkinter GUI.基本上,我不能使用 Tkinter GUI 来调用另一个 Tkinter GUI。 Do you know what I can do different, for instance, can I create the caller GUI using wxPython and have it call all other GUIs that use Tkinter?你知道我可以做些什么不同的,例如,我可以使用 wxPython 创建调用者 GUI 并让它调用所有其他使用 Tkinter 的 GUI 吗?

Thank you!谢谢!

You can't "call" another GUI.您不能“调用”另一个 GUI。 If this other GUI creates its own root window and calls mainloop() , your only reasonable option is to spawn a new process.如果另一个 GUI 创建自己的根窗口并调用mainloop() ,则唯一合理的选择是生成一个新进程。 That's a simple solution that requires little work.这是一个只需很少工作的简单解决方案。 The two GUIs will be completely independent of each other.这两个 GUI 将完全相互独立。

If you have control over the code in both GUIs and you want them to work together, you can make the base class of your GUI a frame rather than a root window, and then you can create as many windows as you want with as many GUIs as you want.如果您可以控制两个 GUI 中的代码并且希望它们一起工作,则可以将 GUI 的基类设为框架而不是根窗口,然后您可以使用任意数量的 GUI 创建任意数量的窗口如你所愿。

For example, let's start with a simple GUI.例如,让我们从一个简单的 GUI 开始。 Copy the following and put it in a file named GUI1.py:复制以下内容并将其放入名为 GUI1.py 的文件中:

import tkinter as tk

class GUI(tk.Frame):
    def __init__(self, window):
        tk.Frame.__init__(self)
        label = tk.Label(self, text="Hello from %s" % __file__)
        label.pack(padx=20, pady=20)

if __name__ == "__main__":
    root = tk.Tk()
    gui = GUI(root)
    gui.pack(fill="both", expand=True)
    tk.mainloop()

You can run that GUI normally with something like python GUI1.py .您可以使用python GUI1.py类的东西正常运行该 GUI。

Now, make an exact copy of that file and name it GUI2.py.现在,制作该文件的精确副本并将其命名为 GUI2.py。 You can also run it in the same manner: python GUI2.py你也可以用同样的方式运行它: python GUI2.py

If you want to make a single program that has both, you can create a third file that looks like this:如果您想制作一个同时具备这两种功能的程序,您可以创建第三个文件,如下所示:

import tkinter as tk
import GUI1
import GUI2

# the first gui owns the root window
win1 = tk.Tk()
gui1 = GUI1.GUI(win1)
gui1.pack(fill="both", expand=True)

# the second GUI is in a Toplevel
win2 = tk.Toplevel(win1)
gui2 = GUI2.GUI(win2)
gui2.pack(fill="both", expand=True)

tk.mainloop()

Depending on your OS and window manager, one window might be right on top of the other, so you might need to move it to see both.根据您的操作系统和窗口管理器,一个窗口可能位于另一个窗口的正上方,因此您可能需要移动它才能看到两个窗口。

Thank you for the ideas.谢谢你的想法。 At first, your code wouldn't print the text on the toplevel window.起初,您的代码不会在顶层窗口上打印文本。 So I edited it a little and it worked!所以我编辑了一点,它奏效了! Thank you.谢谢。 GUI1 and GUI2 look like: GUI1 和 GUI2 看起来像:

import tkinter as tk

def GUI1(Frame):
    label = tk.Label(Frame, text="Hello from %s" % __file__)
    label.pack(padx=20, pady=20)
    return

if __name__ == "__main__":
    root = tk.Tk()
    GUI1(root)
    root.mainloop()

And then the caller looks like this:然后调用者看起来像这样:

from tkinter import *
import GUI1
import GUI2

def call_GUI1():
    win1 = Toplevel(root)
    GUI1.GUI1(win1)
    return

def call_GUI2():
    win2 = Toplevel(root)
    GUI2.GUI2(win2)
    return

# the first gui owns the root window
if __name__ == "__main__":
    root = Tk()
    root.title('Caller GUI')
    root.minsize(720, 600)
    button_1 = Button(root, text='Call GUI1', width='20', height='20', command=call_GUI1)
    button_1.pack()
    button_2 = Button(root, text='Call GUI2', width='20', height='20', command=call_GUI2)
    button_2.pack()
    root.mainloop()

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

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