#!/usr/bin/env python # -*- coding: utf-8 -*- """ 对话框基类 """ import tkinter as tk from .icon_utils import get_icon_path from config.constants import BG_COLOR_DARK import os class BaseDialog(tk.Toplevel): """对话框基类,提供通用的初始化和居中功能""" def __init__(self, parent, title: str, width: int, height: int): """ 初始化对话框 Args: parent: 父窗口 title: 窗口标题 width: 窗口宽度 height: 窗口高度 """ super().__init__(parent) # 先隐藏窗口 self.withdraw() # 设置窗口属性 self.title(title) self.geometry(f"{width}x{height}") self.resizable(False, False) # 设置图标 icon_path = get_icon_path() if os.path.exists(icon_path): self.iconbitmap(icon_path) # 设置深色主题背景 self.configure(bg=BG_COLOR_DARK) # 设置为模态窗口 self.transient(parent) # 居中显示 self._center_window() def _center_window(self): """将窗口居中显示""" self.update_idletasks() screen_width = self.winfo_screenwidth() screen_height = self.winfo_screenheight() window_width = self.winfo_width() window_height = self.winfo_height() x = (screen_width - window_width) // 2 y = (screen_height - window_height) // 2 self.geometry(f"+{x}+{y}") def show(self): """显示对话框""" self.deiconify() self.grab_set() self.wait_window()