127 lines
5.1 KiB
Python
127 lines
5.1 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
UI辅助工具
|
||
负责标签按钮、下拉框等UI组件的辅助功能
|
||
"""
|
||
import customtkinter as ctk
|
||
from config.constants import COLOR_TRANSPARENT
|
||
|
||
|
||
class UIHelpers:
|
||
"""UI辅助工具类,提供各种UI组件的辅助功能"""
|
||
|
||
@staticmethod
|
||
def adjust_tab_button_width(tabview):
|
||
"""调整标签按钮宽度,让它们平分整个宽度"""
|
||
try:
|
||
# 获取tabview的宽度
|
||
tabview_width = tabview.winfo_width()
|
||
|
||
if tabview_width > 1:
|
||
# 获取标签按钮
|
||
segmented_button = tabview._segmented_button
|
||
buttons_dict = segmented_button._buttons_dict
|
||
|
||
# 计算每个按钮的宽度(平分)
|
||
button_count = len(buttons_dict)
|
||
if button_count > 0:
|
||
button_width = tabview_width // button_count
|
||
|
||
# 设置每个按钮的宽度
|
||
for button in buttons_dict.values():
|
||
try:
|
||
button.configure(width=button_width)
|
||
except:
|
||
pass
|
||
|
||
print(f"Adjusted tab button width: {button_width}px (total width: {tabview_width}px)")
|
||
except Exception as e:
|
||
print(f"Failed to adjust tab button width: {e}")
|
||
|
||
@staticmethod
|
||
def fix_dropdown_width(combo_box):
|
||
"""修复下拉菜单宽度,使其与下拉框一致"""
|
||
try:
|
||
# 获取下拉框的实际宽度
|
||
combo_width = combo_box.winfo_width()
|
||
|
||
if combo_width > 1:
|
||
# 方法1: 直接设置 CTkComboBox 的内部属性
|
||
if hasattr(combo_box, '_dropdown_menu'):
|
||
dropdown_menu = combo_box._dropdown_menu
|
||
|
||
if dropdown_menu is not None:
|
||
# 设置下拉菜单的宽度
|
||
try:
|
||
dropdown_menu.configure(width=combo_width)
|
||
except:
|
||
pass
|
||
|
||
# 尝试设置内部 frame 的宽度
|
||
try:
|
||
if hasattr(dropdown_menu, 'winfo_children'):
|
||
for child in dropdown_menu.winfo_children():
|
||
child.configure(width=combo_width)
|
||
except:
|
||
pass
|
||
|
||
# 方法2: 重写 _open_dropdown_menu 方法
|
||
if not hasattr(combo_box, '_original_open_dropdown'):
|
||
combo_box._original_open_dropdown = combo_box._open_dropdown_menu
|
||
|
||
def custom_open_dropdown():
|
||
combo_box._original_open_dropdown()
|
||
# 延迟设置宽度
|
||
if hasattr(combo_box, 'after'):
|
||
combo_box.after(1, lambda: UIHelpers._set_dropdown_width(combo_box, combo_width))
|
||
|
||
combo_box._open_dropdown_menu = custom_open_dropdown
|
||
|
||
except Exception as e:
|
||
print(f"Failed to set dropdown menu width: {e}")
|
||
|
||
@staticmethod
|
||
def _set_dropdown_width(combo_box, width):
|
||
"""设置下拉菜单宽度的辅助方法"""
|
||
try:
|
||
if hasattr(combo_box, '_dropdown_menu') and combo_box._dropdown_menu is not None:
|
||
dropdown = combo_box._dropdown_menu
|
||
dropdown.configure(width=width)
|
||
|
||
# 遍历所有子组件并设置宽度
|
||
for widget in dropdown.winfo_children():
|
||
try:
|
||
widget.configure(width=width)
|
||
except:
|
||
pass
|
||
except:
|
||
pass
|
||
|
||
@staticmethod
|
||
def configure_tab_transparency(project_tab, task_tab):
|
||
"""配置标签页透明度"""
|
||
try:
|
||
# 尝试将标签页背景设置为透明,让圆角容器可见
|
||
project_tab.configure(fg_color=COLOR_TRANSPARENT)
|
||
task_tab.configure(fg_color=COLOR_TRANSPARENT)
|
||
print("[OK] Set tab background to transparent")
|
||
except Exception as e:
|
||
print(f"Failed to set tab background transparent: {e}")
|
||
|
||
@staticmethod
|
||
def hide_scrollbar(scrollable_frame):
|
||
"""隐藏 CTkScrollableFrame 的滚动条(仍保持滚动功能)"""
|
||
try:
|
||
scrollbar = getattr(scrollable_frame, "_scrollbar", None)
|
||
if scrollbar:
|
||
bgcolor = scrollable_frame.cget("fg_color") if hasattr(scrollable_frame, "cget") else COLOR_TRANSPARENT
|
||
scrollbar.configure(fg_color=COLOR_TRANSPARENT, button_color=bgcolor, button_hover_color=bgcolor)
|
||
scrollbar.configure(width=0)
|
||
try:
|
||
scrollbar.grid_remove()
|
||
except Exception:
|
||
pass
|
||
except Exception:
|
||
pass
|