在彼此之上分层图形界面 [英] Layering graphical interfaces on top of each other

查看:18
本文介绍了在彼此之上分层图形界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主程序窗口,带有 framemenu 用于调用计算模块.

I have a main program window with a frame and a menu for invoking calculation modules.

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

# 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")


def start_bolt_connection():
    import Bolt_connection


def start_key_connection():
    import Key_connection


# 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", command=start_bolt_connection)
help_menu2_part_calculation_selection.add_command(label="Key connection", command=start_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_module('Bolt_connection')
root.config(menu=main_menu)
root.mainloop()

我想在程序启动时看到一个看起来像 this 的窗口.然后,当您单击其中一个菜单按钮(例如,Key connection)时,框架应更新并显示新模块的图形界面.点击后窗口应该看起来像this.

I want to see a window that looks like this when the program starts. Then, when you click on one of the menu buttons (for example, Key connection), the frame should update and display the graphical interface of the new module. After clicking the window should look like this.

问题是在显示新模块的图形部分时,旧模块的图形部分没有被擦除,而且它们相互重叠.如何让程序删除旧模块的图形部分?grid_remove()reload() 函数没有帮助.

The problem is that when the graphic part of the new module is displayed, the graphic part of the old module is not erased and they overlap each other. How to make the program delete the graphic part of the old module? The grid_remove() and reload() functions do not help.

关于运行程序时我想得到什么的附加说明.

  1. 程序开始.
  2. 主窗口出现,计算模块已经运行,默认安装.在这种情况下,螺栓连接".窗口现在应该看起来像 this.
  3. 用户从菜单中选择计算模块之一.在这种情况下,密钥连接".先前模块的图形部分被擦除,并显示新模块.窗口现在应该看起来像 this.

UPD

为了更好地理解问题,请添加指向项目文件所在的谷歌驱动器的链接.https://drive.google.com/drive/folders/110OrRqlfzGmswg8cGHO3-C4VxfRj1sos?usp=sharing

For a better understanding of the problem, add a link to google drive where the project files are located. https://drive.google.com/drive/folders/110OrRqlfzGmswg8cGHO3-C4VxfRj1sos?usp=sharing

在这个文件夹中有两个txt文件,里面有导出模块的代码,这样你就不用把项目下载到你的电脑上运行了.

In this folder there are two txt files with the code of the exported modules, so that you do not have to download the project to your computer and run it .

推荐答案

ma​​in.py

import tkinter as tk
import Mod1
import Mod2

root = tk.Tk()
m1 = Mod1.Model(root)
m2 = Mod2.Model(root)
m1.grid(column= 0, row=0)
m2.grid(column = 0 ,row=0)

def callback():
    m1.lift()    
b = tk.Button(text='click me', command=callback)
b.grid(column=0,row=1)

Mod1.py

from __main__ import tk

class Model(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.configure(bg="red", width=300, height=300)

Mod2.py

from __main__ import tk

class Model(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.configure(bg="blue", width=300, height=300)

说明

在您的模块中,您将模型定义为 tk.Frame 的子类,这使您能够像处理 tk.Frame 一样处理模型.

In your Modules you define your Model's as a subclass of tk.Frame, that gives you the ability to handle your Models as same as a tk.Frame.

这篇关于在彼此之上分层图形界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆