使用Tkinter刷新框架

2024-10-04 07:38:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Tkinter中设置一个帧的动画,但我无法这样做。当我想更新滑块时,我会看到一个没有响应的白色屏幕。我也跟着其他例子做了一个实验

self.after(1000, self.UpdateSliders)

app.mainloop()

但是因为我使用的是框架,当点击一个按钮时,我会把它们换掉,所以它不会刷新框架。 如何刷新滑块以在该帧上移动

用于交换帧的代码

# Setup and declaring pages
class SoundBoard(Tk):
def __init__(self, *args, **kwargs):
    Tk.__init__(self, *args, **kwargs)
    
    # Window Config
    self.title("Sound Board")
    self.geometry('975x500')
    self.iconbitmap(FileDir + 'Images/Icons/Black Icon.ico')

    #Setting Up frames for each window
    container = Frame(self)
    container.pack(side="top", fill="both", expand = True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    #Create a blank dictionary which will be populated later
    self.frames = {}

    #Populate dictionary with all the pages (frame) in which the program uses
    for F in (MainWindow,
              ButtonsWindow,
              SliderWindow,
              DisplayWindow,
              ):
        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")
        frame.configure(bg='#1b191a')

    # Show the Main window
    self.show_frame(MainWindow)

# Moves the frame to the front
def show_frame(self, cont):

    frame = self.frames[cont]
    frame.tkraise()

我希望滑块在其中更新的大型机代码

class MainWindow(Frame):
def __init__(self, parent, controller):
    Frame.__init__(self,parent)
    # Set up sliders
    self.Slider1 = Slider(self,100,60,200,15,15,35,0,80,"White","Black")
    self.Slider2 = Slider(self,100,60,200,15,15,35,0,80,"White","Black")
    self.Slider3 = Slider(self,100,60,200,15,15,35,0,80,"White","Black")
    self.Slider4 = Slider(self,100,60,200,15,15,35,0,80,"White","Black")
    self.Slider5 = Slider(self,100,60,200,15,15,35,0,80,"White","Black")
    self.Slider6 = Slider(self,100,60,200,15,15,35,0,80,"White","Black")

    while True:
        self.after(1000, self.UpdateSliders)

# Set value of the sliders
def UpdateSliders(self):
    self.Slider1.SetVal(self.Board1, SavedInfo.ReadSliders("Slider1"))
    self.Slider2.SetVal(self.Board1, SavedInfo.ReadSliders("Slider2"))
    self.Slider3.SetVal(self.Board1, SavedInfo.ReadSliders("Slider3"))
    self.Slider4.SetVal(self.Board1, SavedInfo.ReadSliders("Slider4"))
    self.Slider5.SetVal(self.Board1, SavedInfo.ReadSliders("Slider5"))
    self.Slider6.SetVal(self.Board1, SavedInfo.ReadSliders("Slider6"))
    self.update()
    print("S")

Tags: theselfframesinitcontainerdefframeblack
1条回答
网友
1楼 · 发布于 2024-10-04 07:38:12

我为您编写了一个示例代码,其中包含手动和自动更改选项卡。我希望能从中学到有用的东西

import tkinter as tk

root = tk.Tk()

class Main:
    def __init__(self, master):
        #// Define frames (buttons // content)
        self.frame_tabs = tk.Frame(master)
        self.frame_content = tk.Frame(master)
        #// Pack frames
        self.frame_tabs.grid(row=0, column=0, sticky="nws")
        self.frame_content.grid(row=0, column=1, sticky="nesw")

        #// Define content frames
        self.content_1 = tk.Frame(self.frame_content)
        self.content_2 = tk.Frame(self.frame_content)
        self.content_3 = tk.Frame(self.frame_content)
        self.content_4 = tk.Frame(self.frame_content)
        self.content_5 = tk.Frame(self.frame_content)
        self.content_6 = tk.Frame(self.frame_content)
        self.content_7 = tk.Frame(self.frame_content)
        #// Pack content frames
        self.Frame_Content = [self.content_1, self.content_2, self.content_3, self.content_4, self.content_5, self.content_6, self.content_7]
        for content_f in self.Frame_Content:
            content_f.grid(row=0, column=0, sticky="nesw")

        #// Define tab buttons
        self.tab_1 = tk.Button(self.frame_tabs, command=lambda:Switch_Content(self.content_1, self.tab_1))
        self.tab_2 = tk.Button(self.frame_tabs, command=lambda:Switch_Content(self.content_2, self.tab_2))
        self.tab_3 = tk.Button(self.frame_tabs, command=lambda:Switch_Content(self.content_3, self.tab_3))
        self.tab_4 = tk.Button(self.frame_tabs, command=lambda:Switch_Content(self.content_4, self.tab_4))
        self.tab_5 = tk.Button(self.frame_tabs, command=lambda:Switch_Content(self.content_5, self.tab_5))
        self.tab_6 = tk.Button(self.frame_tabs, command=lambda:Switch_Content(self.content_6, self.tab_6))
        self.tab_7 = tk.Button(self.frame_tabs, command=lambda:Switch_Content(self.content_7, self.tab_7))
        #// Pack tab buttons
        self.Tab_Button = [self.tab_1, self.tab_2, self.tab_3, self.tab_4, self.tab_5, self.tab_6, self.tab_7]
        self.t_index = 0
        for tab in self.Tab_Button:
            # Generate text content
            self.Test_Content(self.Frame_Content[self.t_index],
                              f"Hello my friend!\nThis is content from tab {self.t_index + 1} :)")
            # Tab properties
            tab.configure(bg="lightgray", activebackground="gray", bd=0,
                          font=("Calibri", 12, "bold"), text=f"Tab {self.t_index + 1}")
            tab.grid(row=self.t_index, column=0, sticky="wne")
            self.t_index += 1

        #// Create Auto button for auto switch content
        self.btn_auto = tk.Button(self.frame_tabs, text=f"Auto", font=("Calibri", 12, "bold"),
                                  bg="lightgray", activebackground="gray", relief="groove",
                                  command=lambda: Switch_Auto(this_btn=self.btn_auto, time=500))
        #// Pack: self.t_index on last tab button was increased so will use as reference for this
        #// in case you add other tabs this will be updated to
        self.btn_auto.grid(row=self.t_index, column=0, sticky="wse")

        def Switch_Content(frame, tab):
            for button in self.Tab_Button:
                # Make background light gray for all tabs buttons
                button.configure(bg="lightgray")
                # Change content frame and also change tab button background color
                # like identifier to know on witch tab we are
                frame.tkraise()
                tab.configure(bg="tomato")
        #// Activate Switch effects for first tab by default we se first tab and content
        Switch_Content(self.content_1, self.tab_1)

        self.auto_switch_on = False
        def Switch_Auto(time, this_btn):
            #// Logic to know when to start and stop animation
            #// Basic si just switch form TRUE to FALSE and vice-versa
            if self.auto_switch_on == False:
                self.auto_switch_on = True
                this_btn.configure(bg="green")
            else:
                self.auto_switch_on = False
                this_btn.configure(bg="lightgray")

            self.index = 0
            #// If animation signal is true
            while self.auto_switch_on == True:
                #// If animation signal is false then stop animation
                if self.auto_switch_on == False: break
                #// Switch content
                Switch_Content(self.Frame_Content[self.index], self.Tab_Button[self.index])
                #// Upgrade index
                self.index += 1
                if self.index == len(self.Tab_Button): self.index = 0
                #// master will be in this case root -> "tkinter.Tk()"
                master.after(time)
                master.update()

    #// We create a simple label widget what will reuse it on tab loop pack (just for this example we will generate it)
    def Test_Content(self, frame, text):
        tk.Label(frame, text=text, font=("Comic Sans MS", 18, "bold italic")).grid(row=0, column=0, sticky="nesw")

if __name__ == "__main__":
    Main(root)
    root.mainloop()

我用更多的注释更新了代码,并在激活状态下的自动按钮上更改了颜色

相关问题 更多 >