def start(): app = QSingleApplication("zzclient", sys.argv) # 异常捕获 os.makedirs(Config().except_path, exist_ok=True) sys.excepthook = cgitb.Hook(1, Config().except_path, 5, sys.stderr, '') if app.isRunning(): # 激活窗口 app.sendMessage('show', 1000) else: # 窗口关闭程序退出 app.setQuitOnLastWindowClosed(True) # 应用图标 app.setWindowIcon(ResourceLoader().qt_icon_project_ico) # 设置主题样式 Theme.load() # 设置字体 # fontDB = QFontDatabase() # fontDB.addApplicationFont(':/font/Roboto-Regular.ttf') # app.setFont(QFont('Roboto')) # 设置雅黑字体 app.setFont(ResourceLoader.load_app_font()) # 图标字体 ResourceLoader.load_icon_font() # 加载模块 ResourceLoader.load_modules() # 创建窗体 window = HomePage() window.resize(1024 ,768) # 样式注入器 ConnectStyleSheetInspector(main_window=window, shortcut=QKeySequence(Qt.Key_Tab)) # 计算居中显示的位置 desktop = QDesktopWidget().availableGeometry() width = (desktop.width() - window.width()) / 2 height = (desktop.height() - window.height()) / 2 app.setActivationWindow(window) window.show() window.move(width, height) sys.exit(app.exec_())
class Window(QWidget): def __init__(self, *args, **kwargs): super(Window, self).__init__(*args, **kwargs) self.setStyleSheet(Style) self.resize(1024, 768) self.sheet = ActionSheet(data=['电流', '电压', '电阻'], parent=self) self.sheet.on_item_clicked.connect(lambda item:print('选中了{item}'.format(item=item))) self.sheet.dialog.add_action('重置', lambda :print("点击了重置按钮")) # self.popup = PopupContainer(self, widget=sheet) self.open_drawer() def open_drawer(self): QTimer.singleShot(100, lambda :self.sheet.showSelf()) def closeEvent(self, a0: QtGui.QCloseEvent) -> None: Bloc_App().app_closed.emit() if __name__ == '__main__': app = QApplication(sys.argv) Theme.load() # fontDB = QFontDatabase() # font_id = fontDB.addApplicationFont(':font/Microsoft-YaHei.ttf') # fontName = QFontDatabase.applicationFontFamilies(font_id)[0] # app.setFont(QFont('Microsoft YaHei')) window = Window() window.show() sys.exit(app.exec_())
class BaseView(QWidget): ''' 静态资源管理器 ''' resource: ResourceLoader = ResourceLoader() ''' 路由管理器 ''' router: RouteManager = RouteManager() ''' 全局数据块 ''' global_bloc: Bloc_App = Bloc_App() ''' 配置对象 ''' config = Config() ''' 主题对象 ''' theme = Theme() ''' 图标字体 ''' fontawesome = FontAwesome() def set_style(self): ''' 设置样式 https://stackoverflow.com/questions/31178695/qt-stylesheet-not-working ''' self.setAttribute(QtCore.Qt.WA_StyledBackground) if hasattr(self, 'style_name'): with open(Config().qss_path + '/' + self.style_name + '.qss', 'r', encoding='utf-8') as f: self.setStyleSheet(f.read()) def procedure(self): ''' 初始化流程,set_ui > place > configure > set_signal ''' self.set_style() if hasattr(self, 'set_ui'): self.set_ui() if hasattr(self, 'place'): self.place() if hasattr(self, 'configure'): self.configure() if hasattr(self, 'set_bloc'): self.set_bloc() if hasattr(self, 'set_signal'): self.set_signal() def show_vloading(self): if not hasattr(self, 'inner_loader'): self.inner_loader = Loading(self, modal=False) self.inner_loader.show() def hide_vloading(self): if hasattr(self, 'inner_loader'): self.inner_loader.on_outer_close.emit()