button_styles.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. 统一的按钮样式定义
  3. """
  4. from PyQt5.QtWidgets import QPushButton
  5. from PyQt5.QtGui import QFont
  6. def get_button_style(color_type: str) -> str:
  7. """
  8. 获取按钮样式
  9. :param color_type: 'green' | 'blue' | 'normal'
  10. :return: 样式字符串
  11. """
  12. base_style = "border-radius: 5px; padding: 8px 16px;"
  13. if color_type == 'green':
  14. return f"""
  15. {base_style}
  16. background-color: #4CAF50;
  17. color: white;
  18. border: none;
  19. """
  20. elif color_type == 'blue':
  21. return f"""
  22. {base_style}
  23. background-color: #2196F3;
  24. color: white;
  25. border: none;
  26. """
  27. else: # normal
  28. return f"""
  29. {base_style}
  30. background-color: #f5f5f5;
  31. color: #333;
  32. border: 1px solid #ccc;
  33. """
  34. def apply_button_style(button: QPushButton, color_type: str):
  35. """
  36. 应用按钮样式
  37. :param button: QPushButton实例
  38. :param color_type: 'green' | 'blue' | 'normal'
  39. """
  40. button.setStyleSheet(get_button_style(color_type))
  41. def get_button_color_type(button_text: str) -> str:
  42. """
  43. 根据按钮文本自动判断应该使用哪种颜色
  44. :param button_text: 按钮文本
  45. :return: 'green' | 'blue' | 'normal'
  46. """
  47. # 绿色按钮:确认、保存类操作
  48. green_keywords = ['保存', '确定', '新增', '登录', '提交', '确认']
  49. # 蓝色按钮:主要操作、重要功能
  50. blue_keywords = ['生成', '地磅单', '打印', '读毛重', '读皮重', '当前时间']
  51. for keyword in green_keywords:
  52. if keyword in button_text:
  53. return 'green'
  54. for keyword in blue_keywords:
  55. if keyword in button_text:
  56. return 'blue'
  57. # 默认普通样式
  58. return 'normal'