Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

python - Layering graphical interfaces on top of each other

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

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.

An additional explanation of what I want to get when I run the program.

  1. Program starts.
  2. The main window appears with the calculation module already running, installed by default. In this case, "Bolt connection". The window should now look like this.
  3. The user selects one of the calculation modules from the menu. In this case, "Key connection". The graphic part of the previous module is erased, and the new module is displayed. The window should now look like this.

UPD

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

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 .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

Explaination

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...