python-3.x 当从单独的类调用tkraise()时,

cnwbcb6i  于 4个月前  发布在  Python
关注(0)|答案(1)|浏览(81)

我有一个测试应用程序(下面),导航按钮在一个单独的框架/类中:NavBar()。每个框架和主框架一样也在一个单独的类中。
当我按下按钮时,我希望相应的框架显示在主视图框架中。
我已经构建了下面的代码,它没有给我任何错误,但框架没有被提升到前面。有什么想法我在这里做错了吗?
正如你可能会注意到,我是新的OOP和Tkinter,所以任何建议都欢迎。谢谢
x1c 0d1x的数据

import ttkbootstrap as tb
from ttkbootstrap.constants import *

# run this to see all widgets | python -m ttkbootstrap

class NavBar(tb.Frame):
    def __init__(self, parent, *args, **kwargs):
        tb.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        
        MainViewObject = MainView(self)
             
        txt_logo = tb.Label(
            self, 
            text='LOGO', 
            style='TLabel'

        )
        txt_logo.pack(fill=X, pady=30)
        txt_logo.configure(anchor="center")
        
        nav_separator = tb.Separator(
            self, 
            orient='horizontal', 
            style='secondary.horizontal.TSeparator'
        )
        nav_separator.pack(fill=X)
        
        cb_frame1 = tb.Button(
            master= self,
            text="Frame One",
            width=20,
            command= lambda: MainViewObject.show_frame("frameOneView")
        )
        cb_frame1.pack(fill=X, pady=(30, 0), padx = (10))
        
        
        cb_frame2 = tb.Button(
            master= self,
            text="Frame Two",
            command=lambda: MainViewObject.show_frame("frameTwoView")
        )
        cb_frame2.pack(fill=X, pady=(20,0), padx = (10))
        
        
        cb_frame3 = tb.Button(
            master= self,
            text="Frame Three",
            command=lambda: MainViewObject.show_frame("frameThreeView")
        )
        cb_frame3.pack(fill=X, pady=(20,0), padx = (10))
             
class MainView(tb.Frame):
    def __init__(self, parent, *args, **kwargs):
        tb.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        
        container = tb.Frame(self,bootstyle ="info") # **
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (frameOneView, frameTwoView, frameThreeView):
             frame_name = F.__name__
             frame = F(container, self) # **
             self.frames[frame_name] = frame # **
             frame.grid(row=0, column=0, sticky="nsew")
          
        self.show_frame("frameOneView")
        #print(self, frame_name)
        
    def show_frame(self, frame_name):
        #print(frame_name)
        frame = self.frames[frame_name]
        #print(frame)
        frame.tkraise()

class frameOneView(tb.Frame):
    def __init__(self, parent, controller):
        tb.Frame.__init__(self, parent)
        self.controller = controller
        
        lb1 = tb.Label(self, text="This is the FRAME 1", bootstyle="inverse-dark")
        lb1.pack(side="top", fill="both", pady=10, expand=True)
        lb1.configure(anchor="center")
        
class frameTwoView(tb.Frame):
    def __init__(self, parent, controller):
        tb.Frame.__init__(self, parent)
        self.controller = controller
        
        lb2 = tb.Label(self, text="This is the FRAME 2", bootstyle="inverse-dark")
        lb2.pack(side="top", fill="both", pady=10, expand=True)
        lb2.configure(anchor="center")

class frameThreeView(tb.Frame):
    def __init__(self, parent, controller):
        tb.Frame.__init__(self, parent)
        self.controller = controller
        
        lb3 = tb.Label(self, text="This is the FRAME 3", bootstyle="inverse-dark")
        lb3.pack(side="top", fill="both", pady=10, expand=True)
        lb3.configure(anchor="center")   
         
class MainApplication(tb.Frame):
    def __init__(self, parent, *args, **kwargs):
        tb.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        
        self.navbar = NavBar(self) #,bootstyle ="info")
        self.navbar.pack(side="left", fill="y")
        
        self.main = MainView(self, bootstyle ="sucess")
        self.main.pack(side="right", fill="both", expand=True)

if __name__ == "__main__":
    root = tb.Window(themename="darkly")
    root.state('zoomed')
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

字符串

g9icjywg

g9icjywg1#

您正在创建MainView的两个示例,因此每个内部框架都有两个示例。您在屏幕上看到的一个和按钮触发的一个是两个不同的框架。
你应该只创建一个MainView的示例。你需要删除NavBar中的一个,并让你的导航栏引用另一个。如果你在导航栏之前创建框架,这样它们在你创建导航栏时就存在了,这也会有所帮助。
步骤1:更改在MainApplication中创建项目的顺序:

class MainApplication(tb.Frame):
    def __init__(self, parent, *args, **kwargs):
        tb.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.main = MainView(self, bootstyle ="sucess")
        self.navbar = NavBar(self) #,bootstyle ="info")

        self.navbar.pack(side="left", fill="y")
        self.main.pack(side="right", fill="both", expand=True)

字符串
步骤2:通过删除MainViewObject的定义并将所有使用替换为parent.main,在NavBar中引用此版本的MainView

class NavBar(tb.Frame):
    def __init__(self, parent, *args, **kwargs):
        ...
        cb_frame1 = tb.Button(
            ...,
            command= lambda: parent.main.show_frame("frameOneView")          
        )
        ...

相关问题