首页 > 解决方案 > 在另一个程序模块的程序窗口元素中显示

问题描述

我有一个包含程序窗口基本元素的表单。

from tkinter import Menu, Label, Tk, Frame, Scrollbar, Canvas, N, E, S
import runpy

# Program window dimensions
root = Tk()
root.title("Name")
root.geometry("1260x700")
root.update()
MaxX = root.winfo_width()
MaxY = root.winfo_height()
canvas = Canvas(root, width=MaxX, height=MaxY, bg="white")
scroll_y = Scrollbar(root, orient="vertical", command=canvas.yview)

frame = Frame(canvas)
for i in range(50):
    Label(frame).grid()
    i += 1
canvas.create_window(0, 0, anchor='nw', window=frame)
canvas.update_idletasks()

canvas.configure(scrollregion=canvas.bbox('all'),
                 yscrollcommand=scroll_y.set)

canvas.grid()
scroll_y.grid(row=0, sticky=E + N + S)
main_menu = Menu()

# Submenu for Export to...
file_menu = Menu(tearoff=0)
help_menu = Menu(file_menu, tearoff=0)
help_menu_export_to = Menu(help_menu, tearoff=0)
help_menu_export_to.add_command(label="Excel")
help_menu_export_to.add_command(label="Word")

# Submenu for Part calculation selection
calculation_selection_menu = Menu(tearoff=0)
help_menu2 = Menu(calculation_selection_menu, tearoff=0)
help_menu2_part_calculation_selection = Menu(help_menu2, tearoff=0)
B2 = help_menu2_part_calculation_selection.add_command(label="Bolted (screw) connection")
help_menu2_part_calculation_selection.add_command(label="Key connection")
help_menu2_part_calculation_selection.add_command(label="Pinned connection")

# File menu
file_menu.add_command(label="New")
file_menu.add_command(label="Save")
file_menu.add_cascade(label="Export to...", menu=help_menu_export_to)
file_menu.add_command(label="Open")
file_menu.add_command(label="Exit")

# Main menu
main_menu.add_cascade(label="File", menu=file_menu)
main_menu.add_cascade(label="About")
main_menu.add_cascade(label="Help")
main_menu.add_cascade(label="Part calculation selection", menu=help_menu2_part_calculation_selection)

runpy.run_path('Key_connection.py')
root.config(menu=main_menu)
root.mainloop()

在这个窗口中有一个框架,我想在其中放置将从模块中导出的图形元素。比如我想导出一个关键连接计算模块。我将框架从主窗口导出到这个模块,然后将我需要的所有元素放入其中。例如:

initial_data_frame = LabelFrame(frame, text='Initial data')# The labelframe in which the elements for 
entering the initial data are placed.
initial_data_frame.grid(row=0, column=0, sticky=W + N)
# A picture with explanations of some sizes.
img_1_for_key_connection = PhotoImage(master=frame, file='Key_connection_image_1.png')
img_2_for_key_connection = PhotoImage(master=frame, file='Key_connection_image_2.png')
txt = Text(frame, width=57, height=10)# Text field for outputting data.
txt.grid(row=0, column=0, pady=300, sticky=W + N)

然后我尝试运行该模块:

runpy.run_path('Key_connection.py')

问题是模块在新窗口中打开。 程序显示的示例。 如何使模块出现在主窗口中的框架中? 我想得到的一个例子 提前谢谢你的答案。

标签: pythonuser-interfacetkinter

解决方案


据我了解,您有一个主模块,您在其中定义了要在另一个模块中设置的元素。

在您要导入的模块中,您必须定义一个结构,该结构将从主模块接收框架并用元素填充它。也许一个简单的回调会起作用:

在“子”模块中:

def setup_frame(frame):
    # Add your elements to the frame here...

在主模块中:

import my_submodule  # or whatever module you've defined

my_submodule.setup_frame(frame)

您也可以使用__import__动态执行此操作。


推荐阅读