首页 > 编程问答 >Python、图形用户界面、ctk

Python、图形用户界面、ctk

时间:2024-07-22 16:06:04浏览次数:7  
标签:python tkinter customtkinter

所以,我正在创建一个博客,现在,我在设置部分,我有一个带有按钮的滑动面板,我希望它转到一个新窗口,我将在其中创建新的小部件等...,我已经完成了这种登录和注册的事情,问题是现在我不能使用 pack.forget(),它只是不起作用

class SlidePanel(customtkinter.CTkFrame):
    def __init__(self, parent, start_pos, end_pos):
        super().__init__(master=parent, fg_color="white")

        # Attributes
        self.start_pos = start_pos
        self.end_pos = end_pos
        self.width = abs(start_pos - end_pos)

        # Animated attributes
        self.pos = start_pos
        self.in_start_pos = True

        # Layout
        self.place(relx=self.start_pos, rely=0.05, relwidth=0.25, relheight=0.9)  # Adjust the width to 0.25

        # Add buttons
        self.add_buttons()

    def add_buttons(self):
        button_explore = customtkinter.CTkButton(self, text="Explore", command=self.on_explore)
        button_profile = customtkinter.CTkButton(self, text="Profile", command=self.on_profile)
        button_settings = customtkinter.CTkButton(self, text="Settings", command=self.on_settings)

        button_explore.pack(expand=True, pady=7)
        button_profile.pack(expand=True, pady=7)
        button_settings.pack(expand=True, pady=7)

    def on_explore(self):
        print("Explore button clicked")

    def on_profile(self):
        print("Profile button clicked")

    def on_settings(self):
        open_settings_window()

    def animate(self):
        if self.in_start_pos:
            self.animate_forward()
        else:
            self.animate_backward()

    def animate_forward(self):
        if self.pos < self.end_pos:
            self.pos += 0.005  # Increase the step size for faster animation
            self.place(relx=self.pos, rely=0.05, relwidth=0.25, relheight=0.9)  # Adjust the width to 0.25
            self.after(5, self.animate_forward)
        else:
            self.in_start_pos = False

    def animate_backward(self):
        if self.pos > self.start_pos:
            self.pos -= 0.005  # Increase the step size for faster animation
            self.place(relx=self.pos, rely=0.05, relwidth=0.25, relheight=0.9)  # Adjust the width to 0.25
            self.after(5, self.animate_backward)
        else:
            self.in_start_pos = True

def open_settings_window():
    for widget in window.winfo_children():
        widget.pack_forget()

    window.title("Settings Hub")  # Update title

    # Create the new widgets for the settings page
    label = customtkinter.CTkLabel(window, text="test",)
    label.pack(pady=10)

我期待它有点刷新,我们会说,给我一个干净的窗口,带有新标题,里面有一个标签供测试


问题出在的 open_settings_window 函数试图在循环中使用 pack_forget() 的同时删除小部件。在 Tkinter 中( customtkinter 是建立在 Tkinter 之上的),在迭代小部件集合的同时修改它可能会导致意外行为。

与其尝试一次性删除所有小部件,不如考虑为的设置页面创建一个单独的框架,并在打开设置窗口时隐藏主要内容框架并显示设置框架。

以下是如何修改代码以实现此目的的方法:

import customtkinter

class SlidePanel(customtkinter.CTkFrame):
    # ...(代码与之前相同)...

def open_settings_window():
    global content_frame  # Make content_frame accessible
    content_frame.pack_forget()  # Hide the main content
    settings_frame.pack(fill="both", expand=True)  # Show the settings frame

# --- Main application ---
window = customtkinter.CTk()
window.geometry("800x600")

# Create a frame for the main content
content_frame = customtkinter.CTkFrame(window)
content_frame.pack(fill="both", expand=True)

# Create the settings frame but don't pack it yet
settings_frame = customtkinter.CTkFrame(window)
label = customtkinter.CTkLabel(settings_frame, text="test")
label.pack(pady=10)

slide_panel = SlidePanel(window, start_pos=-0.25, end_pos=0)
window.mainloop()

说明:

  1. 全局 content_frame 我们使 content_frame 成为全局变量,以便可以在 open_settings_window 中访问它。
  2. content_frame settings_frame 我们创建了两个框架:一个用于应用程序的主要内容( content_frame ),另一个用于设置页面( settings_frame )。
  3. 最初隐藏 settings_frame 我们创建了 settings_frame ,但没有使用 pack 将其添加到窗口中,因此它最初是隐藏的。
  4. open_settings_window 中切换框架: 在此函数中,我们现在隐藏 content_frame 并显示 settings_frame ,从而有效地切换到设置页面。

通过这种方法,可以避免在迭代时从窗口中删除小部件的问题,并创建一个更清晰的应用程序布局。

标签:python,tkinter,customtkinter
From: 78777527

相关文章

  • 当我的代码损坏时,如何设置警报或蜂鸣声? (最好是Python)
    我正在为机器人运行一些代码,它将继续运行,直到我手动终止该进程。或者,如果代码意外遇到诸如SYntaxError或其他此类错误/异常之类的错误并崩溃。我想知道当我的代码崩溃时是否可以设置一些警报或蜂鸣声。我的目标就是将视线从屏幕上移开,仅在进程停止运行时才检查它。如果......
  • VScode连接虚拟机运行Python文件的方法
    声明:本文使用Linux发行版本为rocky_9.4目录1.在rocky_9.4最小安装的系统中,默认是没有tar工具的,因此,要先下载tar工具2.在安装好的vscode中下载ssh远程插件工具3.然后连接虚拟机4.查看python是否已经安装5.下载扩展插件6.新建.py文件测试1.在rocky_9.4最小安装......
  • 【介绍Python多进程】
    ......
  • 用python制作终端向上滑动的效果
    我正在开发一个项目,需要时需要过渡效果。我正在通过CRT终端模拟器“cool-retro-term”运行这个python项目。我想让它像老式CRT终端一样,屏幕平滑地向上滑动所有字符以呈现下一行或刷新屏幕。像这样:终端滑动效果不是单独打印的字符,而是屏幕的滚动。到目......
  • centos stream9(linux): 编译安装python 3.12.4
    一,官方下载地址:https://www.python.org/downloads/点击进入具体版本的下载页面,我们选择稳定版本,地址:https://www.python.org/downloads/release/python-3124/如图:复制得到下载链接:https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz 二,下载:从命令行下载:......
  • 使用 beautifulsoup python 更改内部标签的文本
    我想更改使用Beautifulsoup获得的HTML中标签的内部文本。示例:<ahref="index.html"id="websiteName">Foo</a>变成:<ahref="index.html"id="websiteName">Bar</a>我已经设法通过其id获取标签:HTMLDocument.find(id......
  • Python - Adob​​e InDesign Javascript 脚本帮助从 Python 调用 JSX
    提前致谢。希望每个人都表现出色。我试图从python调用Adob​​eIndesignJSX文件,下面是示例代码:我想在Adob​​eINdesign2024或更高版本上运行它。我在PythonInDesign脚本编写上看到了一些示例:从预检中获取溢出文本框以自动调整大小作为参考,可能适用于Ado......
  • 为什么将小部件添加到滚动视图在 python kivy 中不起作用
    Python文件fromkivymd.appimportMDAppfromkivy.langimportBuilderfromkivy.uix.floatlayoutimportFloatLayoutfromkivy.core.windowimportWindowfromkivy.configimportConfigfromkivymd.uix.listimportOneLineListItem#UkuranwindowConfig.set(&......
  • Python 实现Excel和TXT文本格式之间的相互转换
    Excel是一种具有强大的数据处理和图表制作功能的电子表格文件,而TXT则是一种简单通用、易于编辑的纯文本文件。将Excel转换为TXT可以帮助我们将复杂的数据表格以文本的形式保存,方便其他程序读取和处理。而将TXT转换为Excel则可以将文本文件中的数据导入到Excel中进行进一步的分析和......
  • Python (Django) 数据操作
    “如何将Excel考勤数据转换为特定的数据库插入格式?”**我Excel中的数据如下所示:**这是数据格式I**需要将其转换为适合数据库插入的格式,如下所示:**我想要的数据将Excel考勤数据转换为特定数据库插入格式的Python......