当我在 customtkinter 中调用小部件时,如何阻止它们在新窗口中打开?

How do I stop my widgets from opening in a new window when I call them in customtkinter?

提问人:char_par 提问时间:11/15/2023 最后编辑:JRiggleschar_par 更新时间:11/15/2023 访问量:16

问:

以下代码是为学校项目创建 GUI 的更广泛程序的一部分,我不知道为什么类 PreferencesGUI() 没有将自己定位在我在 baseSetup 类中创建的窗口中?

class baseSetup(customtkinter.CTk):
    def __init__(self):
        super().__init__()


        #MAIN SETUP
        self.title("Recs")
        self.geometry("898x506")
        
        self.grid_columnconfigure((1),weight=0)
        self.grid_columnconfigure((2,3,4,5),weight=0)
        self.grid_rowconfigure((0,1,2,3,4),weight=0)
        self.grid_rowconfigure((15),weight=1)

        #create menu panel on left of screen
        self.frameLeft = customtkinter.CTkFrame(self, width=140, corner_radius=0)
        self.frameLeft.grid(row=0, column=0, rowspan=16, sticky="nsew")
        self.frameLeft.grid_rowconfigure(4, weight=1)
        self.frameLeftMenuTitle = customtkinter.CTkLabel(self.frameLeft, text="TITLE", font=("Arial", 40))
        self.frameLeftMenuTitle.grid(row=0, column=0, padx=20, pady=(20, 10))

        self.frameLeftHomeButton = customtkinter.CTkButton(self.frameLeft,text="Home", font=("Arial",16), command=buttonPressHome)
        self.frameLeftHomeButton.grid(row=1,column=0,padx=30, pady=(20, 10))

        self.frameLeftPreferencesButton = customtkinter.CTkButton(self.frameLeft,text="Preferences", font=("Arial",16), command=buttonPressPreferences)
        self.frameLeftPreferencesButton.grid(row=2,column=0,padx=30, pady=(20, 10))

        self.frameLeftInfoButton = customtkinter.CTkButton(self.frameLeft,text="Info", font=("Arial",16), command=buttonPressInfo)
        self.frameLeftInfoButton.grid(row=3,column=0,padx=30, pady=(20, 10))

        self.frameLeftAccountButton = customtkinter.CTkButton(self.frameLeft,text="Account", font=("Arial",16), command=buttonPressAccount)
        self.frameLeftAccountButton.grid(row=4,column=0,padx=30, pady=(20, 10),sticky="new")


        self.mainloop()


class PreferencesGUI(customtkinter.CTk):
    def __init__(self):
        super().__init__()   

        #create central part of preferences gui, with sliders, tickboxes, gui mode, account management, etc.
        frameMiddleWidth = 100
        min = 0
        max = 100
        startVal = int(max/2)
        sliderLabelPadyTop=21
        sliderLabelPadyBot=0
        sliderBoxPadyTop=0
        sliderBoxPadyBot=36

        self.frameMaster = customtkinter.CTkScrollableFrame(self,width=675, corner_radius=0,fg_color="transparent")
        self.frameMaster.grid(column=1,row=0,rowspan=16,columnspan=5,sticky="nsew",)

        #top frame with title in
        self.frameMiddle = customtkinter.CTkFrame(self.frameMaster,corner_radius=10)
        self.frameMiddle.grid(row=0, column=1, rowspan=1, columnspan=5, padx=(10,10), pady=(10,10), sticky="nsew")
        self.frameMiddle.grid_rowconfigure(1,weight=1)
        self.frameMiddleTitle = customtkinter.CTkLabel(self.frameMiddle, text="Preferences",font=("Arial", 40))
        self.frameMiddleTitle.grid(row=0, column=2, padx=150, pady=(10, 10), columnspan=5)  

这是我运行代码时的样子

这就是我想要的样子

我最初只有 1 个类,但是当我添加功能等时,处理起来会变得非常烦人。

python oop 继承 customtkinter

评论


答:

0赞 JRiggles 11/15/2023 #1

问题是它继承自 就像你的主窗口一样,所以当你调用 时,它会创建另一个实例。PreferencesGUIcustomtkinter.CTkbaseSetupsuper().__init__()PreferencesGUICTk

如果你想充当 的子项,你可能想继承类似的东西,而不是 。这样,您就可以从 内部实例化。您尚未显示实例化的位置,但可能需要将其移动到主类中。PreferencesGUIbaseSetupFrameCTkPreferencesGUIbaseSetupPreferencesGUI

import tkinter as tk  # import tkinter to give us access to the tk.Frame widget class

class baseSetup(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        # instantiate PreferencesGUI passing 'self' (this class) as the master
        self.prefs_gui = PreferencesGUI(self)
        # add prefs_gui to the UI
        self.prefs_gui.pack()
        ...  # code omitted for brevity
        self.mainloop()


class PreferencesGUI(tk.Frame):  # inherit from Frame here instead, for example
    def __init__(self, master):
        super().__init__(master)  # pass the same 'master' to the Frame superclass
        ...  # code omitted for brevity

FWIW,您不必继承自 ,但就“通用”容器小部件而言,这可能是最容易处理的事情tk.Frame