首页 > 解决方案 > 带框架的窗口切换

问题描述

大家晚上好

我在开发一个软件,知道我是初学者,我在网上通过看2-3个视频来帮助自己。我的软件由一个带有 3 个工具(拆分/融合/反重复)的主窗口组成

我已经开始使用 (TopLevel) 制作窗口,但它给我带来了很多问题,我不会详细说明。

现在我只是在我的第二个窗口中使用 (Toplevel)。但我希望它切换到一个框架:

我查看了很多与此类似的代码:

https://pythonprogramming.net/change-show-new-frame-tkinter/

我已经尝试了很多次,我只是无法适应我的代码。


import os
import tkinter as tk
import webbrowser 
from tkinter import messagebox
import subprocess
import time
from tkinter import ttk
from tkinter.filedialog import askopenfilename

#----------------------------------------------------------

def OpenFile1():

   name1 = askopenfilename(initialdir="/",
                          filetypes =(("Text File", "*.txt"),("All Files","*.*")),
                          title = "Choose a file."
                          )
   print (name1)
   #Using try in case user types in unknown file or closes without choosing a file.
   try:
       with open(name1,'r') as UseFile:
           print(name1)
   except:
       print("No file exists")

#----------------------------------------------------------

class HoverButton(tk.Button):
   def __init__(self, master, **kw):
       tk.Button.__init__(self,master=master,**kw)
       self.defaultBackground = self["background"]
       self.bind("<Enter>", self.on_enter)
       self.bind("<Leave>", self.on_leave)

   def on_enter(self, e):
       self['background'] = self['activebackground']

   def on_leave(self, e):
       self['background'] = self.defaultBackground

#----------------------------------------------------------

def Anti_Duplicate():

   A_Duplicate = tk.Toplevel()
   A_Duplicate.resizable(width=False, height=False)
   screenn_x = int(A_Duplicate.winfo_screenwidth())
   A_Duplicate.config(background='#1c2028')
   screenn_y = int(A_Duplicate.winfo_screenheight()) 
   A_Duplicate.title("Anti-Duplicate v0.0.1")
   A_Duplicate.resizable(width=False, height=False)
   windowss_x = 570
   windowss_y = 340

   possX = (screenn_x // 2) - (windowss_x // 2)
   possY = (screenn_y // 2) - (windowss_y // 2)

   geoo = "{}x{}+{}+{}".format(windowss_x, windowss_y, possX, possY)
   A_Duplicate.geometry(geoo)

   mainframe = tk.Frame(A_Duplicate, bg='#1c2028')
   mainframe.pack(side= "top", ipadx= 5, ipady= 5)

   bouton_1 = HoverButton(A_Duplicate, font=("Arial", 10), text="Back", background='#1c2028', fg='white',  borderwidth=2, activebackground= '#202124', activeforeground='#1195cf', relief='ridge', command= A_Duplicate.destroy)
   bouton_1.place(x=520, y=300)

   open_button = HoverButton(A_Duplicate, font=("Arial", 10), text="Ouvrir un fichier..", background='#1c2028', fg='white',  borderwidth=2, activebackground= '#202124', activeforeground='#1195cf', relief='ridge', command= OpenFile1)
   open_button.place(x=229.5, y=200)

   bouton_2 = tk.Button(A_Duplicate, font=("Arial", 10), text="Cet outil a pour but de supprimer les lignes en double d'un fichier texte.",  background='#202124', fg='#1195cf',  borderwidth=2, activebackground= '#202124', activeforeground='#1195cf', relief='sunken')
   bouton_2.place(x=75.5, y=50)

   bouton_1 = tk.Button(mainframe, font=("Arial", 15), text="Anti-Duplicate", background='#202124', fg='#1195cf',  borderwidth=2, activebackground= '#202124', activeforeground='#1195cf', relief='sunken')
   bouton_1.pack(padx= 5, pady=10, ipadx= 30)


#Couleur sur le bouton

def main_menu():

   main_screen = tk.Tk()
   main_screen.lift()
   screenn_x = int(main_screen.winfo_screenwidth())
   main_screen.config(background='#1c2028')
   screenn_y = int(main_screen.winfo_screenheight()) 
   main_screen.title("ComboKit v0.0.1")
   windowss_x = 570
   windowss_y = 340

   possX = (screenn_x // 2) - (windowss_x // 2)
   possY = (screenn_y // 2) - (windowss_y // 2)

   geoo = "{}x{}+{}+{}".format(windowss_x, windowss_y, possX, possY)
   main_screen.geometry(geoo)

   mainframe = tk.Frame(main_screen, bg='#1c2028')
   mainframe.pack(side= "top", ipadx= 5, ipady= 5)

   bouton_1 = HoverButton(mainframe, font=("Arial", 15), text="Fusion", background='#1c2028', fg='white',  borderwidth=2, activebackground= '#202124', activeforeground='#1195cf', relief='ridge', command=None)
   bouton_1.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   bouton_2 = HoverButton(mainframe, font=("Arial", 15), text="Anti-Duplicate", background='#1c2028', fg='white',  borderwidth=2, activebackground= '#202124', activeforeground='#1195cf', relief='ridge', command=Anti_Duplicate)
   bouton_2.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   bouton_3 = HoverButton(mainframe, font=("Arial", 15), text="Split ", background='#1c2028', fg='white',  borderwidth=2, activebackground= '#202124', activeforeground='#1195cf', relief='ridge', command=None)
   bouton_3.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   main_screen.mainloop()

main_menu()

非常感谢会帮助我的人!

标签: pythontkinter

解决方案


只需对代码进行少量修改即可满足您的要求:

  • 删除所有Toplevel相关的东西Anti_Duplicate()
  • grid(...)mainframe里面使用main_menu()Anti_Duplicate()
  • 添加main_screen.rowconfigure(0, weight=1)并填充根窗口中的可用main_screen.columnconfigure(0, weight=1)空间mainframe

以下是修改后的main_menu()and Anti_Duplicate()

def Anti_Duplicate():
   mainframe = tk.Frame(bg='#1c2028')
   mainframe.grid(row=0, column=0, sticky='nsew') # use grid to overlay frame in same cell

   bouton_1 = HoverButton(mainframe, font=("Arial", 10), text="Back",
                          background='#1c2028', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=mainframe.destroy)  # destroy mainframe
   bouton_1.place(x=520, y=300)

   open_button = HoverButton(mainframe, font=("Arial", 10), text="Ouvrir un fichier..",
                             background='#1c2028', fg='white', borderwidth=2,
                             activebackground='#202124', activeforeground='#1195cf',
                             relief='ridge', command=OpenFile1)
   open_button.place(relx=.5, y=200, anchor='n')

   bouton_2 = tk.Button(mainframe, font=("Arial", 10),
                        text="Cet outil a pour but de supprimer les lignes en double d'un fichier texte.",
                        background='#202124', fg='#1195cf', borderwidth=2,
                        activebackground= '#202124', activeforeground='#1195cf', relief='sunken')
   bouton_2.place(relx=.5, y=50, anchor='n')

   bouton_1 = tk.Button(mainframe, font=("Arial", 15), text="Anti-Duplicate",
                        background='#202124', fg='#1195cf',  borderwidth=2,
                        activebackground='#202124', activeforeground='#1195cf', relief='sunken')
   bouton_1.pack(padx=5, pady=10, ipadx= 30)

def main_menu():
   main_screen = tk.Tk()
   main_screen.lift()
   screenn_x = int(main_screen.winfo_screenwidth())
   main_screen.config(background='#1c2028')
   screenn_y = int(main_screen.winfo_screenheight()) 
   main_screen.title("ComboKit v0.0.1")
   windowss_x = 570
   windowss_y = 340

   possX = (screenn_x // 2) - (windowss_x // 2)
   possY = (screenn_y // 2) - (windowss_y // 2)

   geoo = "{}x{}+{}+{}".format(windowss_x, windowss_y, possX, possY)
   main_screen.geometry(geoo)

   # make frame at row=0 and column=0 to fill available space in root window    
   main_screen.rowconfigure(0, weight=1)
   main_screen.columnconfigure(0, weight=1)

   mainframe = tk.Frame(main_screen, bg='#1c2028')
   mainframe.grid(row=0, column=0, sticky='n') # use grid() instead of pack()

   bouton_1 = HoverButton(mainframe, font=("Arial", 15), text="Fusion",
                          background='#1c2028', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=None)
   bouton_1.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   bouton_2 = HoverButton(mainframe, font=("Arial", 15), text="Anti-Duplicate",
                          background='#1c2028', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=Anti_Duplicate)
   bouton_2.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   bouton_3 = HoverButton(mainframe, font=("Arial", 15), text="Split",
                          background='#1c2028', fg='white', borderwidth=2,
                          activebackground='#202124', activeforeground='#1195cf',
                          relief='ridge', command=None)
   bouton_3.pack(side= "left", padx= 5, pady=5, ipadx= 30)

   main_screen.mainloop()

推荐阅读