这是我的第一个 GUI:
#GUI1
import random, Id_generator, tkinter as tk
from tkinter import ttk
#constanst
STUDENTS = 15
TEACHERS = 4
def main():
#Main Window
app = tk.TK()
app.title('GUI1')
app.geometry('1000x600')
more code here...
app.mainloop()
def other_function()
code...
这是我的第二个 GUI2
#GUI2
import GUI1, tkinter as tk,
from tkinter import ttk
#window
app = tk.Tk()
app.title('GUI2')
app.geometry('1400x800')
label_1 = ttk.Label(app, text ='imported gui')
label_1.pack()
gui_frame = tk.Frame(app)
gui_frame.pack(expand = True, fill = 'both')
gui_1_instance = GUI1.main()
gui_1_instance.pack(in_ = frame, expand = True, fill = 'both'
我想将GUI1嵌入到GUI2中,这样当我运行GUI2时,GUI1将显示在GUI2的窗口中。这是我到目前为止所尝试过的:将 GUI1 中的 app.mainloop() 语句更改为 return(app)。到目前为止我还没有成功。每当我运行 GUI2 时,两个窗口都会打开 GUI1 和 GUI2,但 GUI1 不会打包到 GUI2 中。
如何在 GUI 中嵌入 GUI? Tkinter 可以做到这一点吗?或者我应该切换到 PYQT?
将 tkinter GUI 嵌入到另一个 tkinter GUI 中非常容易。由于您的代码不完整,我在这里展示另一个示例。
from tkinter import *
#GUI to be embedded
def embed_gui(parent):
frame = Frame(parent)
frame.pack(side='top')
label = Label(frame, text="This is the embedded GUI")
label.pack(side='top')
btn = Button(frame, text="Submit")
btn.pack(side='top')
#GUI in which embedded into
def main():
root = Tk()
root.geometry('500x400')
# Embed using the function embed_gui
embed_gui(root)
root.mainloop()
#run main() if is the main function and not the imported one.
if __name__ == "__main__": main()