使用 Python,如何从另一个 GUI 调用 tkinter GUI? [英] Using Python, how do you call a tkinter GUI from another GUI?
问题描述
我使用 tkinter 创建了几个 GUI.但现在我有兴趣将它们组合成一个调用者 GUI.因此,调用方 GUI 将具有按钮,单击这些按钮时会打开其他 GUI.但是,我无法让它工作.我已经正确地完成了导入(我认为),编辑了 subGUI 中的主要功能,并在我的按钮中添加了 command=GUI.main.我让它加载,但我收到有关丢失文件的错误...但是当我单独运行 GUI 时它工作正常.
I created a couple of GUIs using tkinter. But now I am interested in combining them into one caller GUI. So the caller GUI would have buttons that, when clicked, would open the other GUIs. 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. I get it to load but I get errors about missing files...but when I run a GUI by itself it works fine.
在我的研究中,我读到 Tkinter 程序中只能有一个主循环.基本上,我不能使用 Tkinter GUI 来调用另一个 Tkinter GUI.你知道我可以做些什么不同的,例如,我可以使用 wxPython 创建调用者 GUI 并让它调用所有其他使用 Tkinter 的 GUI 吗?
In my research, I read that there can only be one mainloop in a Tkinter program. Basically, I cannot use a Tkinter GUI to call another 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?
谢谢!
推荐答案
谢谢你的想法.起初,您的代码不会在顶层窗口上打印文本.所以我编辑了一点,它奏效了!谢谢你.GUI1 和 GUI2 看起来像:
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:
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()
这篇关于使用 Python,如何从另一个 GUI 调用 tkinter GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!