图形界面彼此层叠 [英] Layering graphical interfaces on top of each other

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

问题描述

我有一个带有框架菜单.

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()

我希望在程序启动时看到一个类似于的窗口.然后,当您单击菜单按钮之一(例如,键连接")时,框架将更新并显示新模块的图形界面.单击窗口后,外观应为.

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.

问题是,当显示新模块的图形部分时,旧模块的图形部分不会被擦除,它们

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. 出现主窗口,其中计算模块已在运行,默认情况下已安装.在这种情况下,螺栓连接".窗口现在应该看起来像.
  3. 用户从菜单中选择一个计算模块.在这种情况下,键连接".先前模块的图形部分将被删除,并显示新模块.窗口现在应该看起来像.

UPD

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

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 .

推荐答案

main.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天全站免登陆