def makeCenter(self): ''' 使主界面居中 ''' cp = QDesktopWidget().availableGeometry() self.resize(int(cp.width()*0.85), int(cp.height()*0.9)) qr = self.frameGeometry() qr.moveCenter(cp.center()) self.move(qr.topLeft())
def resize(self, ratio=0.8): desktop_rect = QDesktopWidget().availableGeometry() cp = desktop_rect.center() new_width = desktop_rect.width() * ratio new_height = desktop_rect.height() * ratio new_left = cp.x() - new_width // 2 new_top = cp.y() - new_height // 2 new_rect = QRect(new_left, new_top, new_width, new_height) self.setGeometry(new_rect)
class my_window(QWidget): def __init__(self, parent): super().__init__(parent) def initUI(self, w_factor, h_factor): self.size_window(w_factor, h_factor) self.centre_window() self.define_colors() self.color_window() self.add_widgets() def determine_geometry(self): self.geo = QDesktopWidget().availableGeometry() def add_icon(self, string): self.icon = QIcon() self.icon.addFile(string) self.setWindowIcon(self.icon) def define_colors(self): self.window_color = "#DEDEDE" self.rocket_red = "#C20024" self.grey = "#4D4D4D" def color_window(self): pal = QPalette() my_window_color = QColor() my_window_color.setNamedColor(self.window_color) pal.setColor(QPalette.Window, my_window_color) self.setPalette(pal) def size_window(self, w_factor, h_factor): self.w = self.geo.width() / w_factor self.h = self.geo.height() / h_factor self.resize(self.w, self.h) def centre_window(self): centre = self.geo.center() x = centre.x() y = centre.y() self.move(x - self.w / 2, y - self.h / 2) def create_grid(self): self.grid = QGridLayout() self.setLayout(self.grid) def animate_svg(self, svg_image): svg_image.opac_eff = QGraphicsOpacityEffect() svg_image.setGraphicsEffect(svg_image.opac_eff) svg_image.animation = QPropertyAnimation(svg_image.opac_eff, b"opacity") svg_image.animation.setDuration(2000) svg_image.animation.setStartValue(0.1) svg_image.animation.setEndValue(1) svg_image.animation.setEasingCurve(QEasingCurve.OutCirc) svg_image.animation.start()
class MainWidget(QWidget): def __init__(self, section: Section): super().__init__() self.__screen_size = QDesktopWidget().screenGeometry(-1) self.__title = QLabel(section.title, self) self.__title.setFont(QFont('Ubuntu Mono', self.__screen_size.height() * 0.06)) self.__field = GameFieldWidget(self.__screen_size.height() * GAME_FIELD_SIZE_COFF, self.__screen_size.height() * GAME_FIELD_SIZE_COFF / 3, section, self.show_teacher_info, self) self.__field.move(self.__screen_size.center() - self.__field.rect().center()) self.__current_teacher_widget = None p = self.palette() p.setColor(self.backgroundRole(), QColor(250, 205, 140)) self.setPalette(p) self.showMaximized() self.__title.move(self.__screen_size.width() / 2 - self.__title.width() / 2, self.__title.height() / 40 - 10) def mousePressEvent(self, _) -> None: if self.__current_teacher_widget is None: return self.__current_teacher_widget.setParent(None) self.__current_teacher_widget = None self.__field.show() self.__title.show() def show_teacher_info(self, teacher: Teacher): self.__current_teacher_widget = TeacherInfoWidget(teacher, TEACHER_INFO_SIZE, self) self.__field.hide() self.__title.hide() self.__current_teacher_widget.show() self.__current_teacher_widget.move( self.__screen_size.center() - self.__current_teacher_widget.rect().center())
def startGui(self): geo = QDesktopWidget().availableGeometry() screenWidth = geo.width() screenHeight = geo.height() width = int(screenWidth * 0.2) height = int(screenHeight * 0.2) self.resize(width, height) frameGeo = self.frameGeometry() cp = geo.center() frameGeo.moveCenter(cp) self.move(frameGeo.topLeft()) self.show()
class MainWindowTest(unittest.TestCase): def setUp(self): self.app = QApplication([]) self.main_window = MainWindow() self.desktop = QDesktopWidget().availableGeometry() def tearDown(self): self.app.deleteLater() def test_width_height(self): self.assertEqual(self.main_window.width(), self.desktop.width() - 100) self.assertEqual(self.main_window.height(), self.desktop.height() - 100) def test_minimum_width_height(self): self.assertEqual(self.main_window.minimumWidth(), 800) self.assertEqual(self.main_window.minimumHeight(), 600) def test_center_point(self): x = self.main_window.geometry().center().x() y = self.main_window.geometry().center().y() x_desktop = self.desktop.center().x() y_desktop = self.desktop.center().y() # The main window's center point is set like the half of width or height of desktop # Therefore, variation between them must be less 1 self.assertTrue(abs(x - x_desktop) <= 1) self.assertTrue(abs(y - y_desktop) <= 1) def test_setting_menubar(self): # Test that menubar was set. If it's new QMainWindow, then its menu bar must be None self.assertIsNotNone(self.main_window.menuWidget()) self.assertIsNone(QMainWindow().menuWidget()) def test_setting_title_parameters(self): self.assertEqual(self.main_window.windowTitle(), 'Troops review') # Check default icon path is a file self.assertTrue(isfile(self.main_window.ICON_PATH)) # It's necessary to not print an error to the console @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_setting_central_widget(self, string_io): # The central widget isn't set at the moment self.assertIsNone(self.main_window.centralWidget()) self.main_window.setup_central_widget('first', 'second') # But now it's set self.assertIsNotNone(self.main_window.centralWidget())
def __init__(self, *args, **kwargs): super(BaseWindow, self).__init__(*args, **kwargs) # self.mousePressed = False # 设置窗体的图标和名称 self.setWindowIcon(QIcon("media/logo.png")) self.setWindowTitle("瑞达期货研究院分析决策系统") # 标题栏 self.title_bar = TitleBar(parent=self) # 导航栏 self.navigation_bar = NavigationBar(parent=self) # 导航栏的信号 self.navigation_bar.clicked_login_button.connect(self.user_to_login) self.navigation_bar.clicked_register_button.connect(self.user_to_register) self.navigation_bar.clicked_logout_button.connect(self.user_to_logout) self.navigation_bar.module_bar.menu_clicked.connect(self.module_clicked) # 选择了某个模块的 self.navigation_bar.permit_bar.to_usercenter.connect(self.skip_to_usercenter) # 跳转至用户中心 # 窗口承载体 self.page_container = LoadedPage(parent=self) # 属性、样式 user_desktop = QDesktopWidget().availableGeometry() # 用户的桌面信息,来改变自身窗体大小 max_width = user_desktop.width() max_height = user_desktop.height() self.resize(max_width * 0.8, max_width * 0.8 * 0.618) self.setMaximumSize(max_width, max_height) # 最大为用户桌面大小 self.setMinimumSize(max_width * 0.5, max_height * 0.5) # 最小为用户桌面大小的一半 my_frame = self.frameGeometry() # 1 (三步法放置桌面中心)自身窗体信息(虚拟框架) my_frame.moveCenter(user_desktop.center()) # 2 框架中心移动到用户桌面中心 self.move(my_frame.topLeft()) # 3 窗口左上角与虚拟框架左上角对齐 self.setWindowFlags(Qt.FramelessWindowHint) # 无边框 self.setAttribute(Qt.WA_TranslucentBackground, True) # 背景全透明(影响子窗口) self._pressed = False self._direction = None self._mouse_pos = None self.setMouseTracking(True) # 鼠标不点下移动依然有效(针对本窗口, 子控件无效) self.title_bar.installEventFilter(self) # 子控件安装事件事件过滤 self.navigation_bar.installEventFilter(self) self.page_container.installEventFilter(self) # 布局 layout = QVBoxLayout(margin=self.MARGIN, spacing=0) layout.addWidget(self.title_bar) layout.addWidget(self.navigation_bar) layout.addWidget(self.page_container) self.setLayout(layout) self.navigation_bar_channel = NavigationBarChannel()
def __init__(self): super().__init__() # Create window at center of the display. display_geometry = QDesktopWidget().availableGeometry() window_size = display_geometry.size() / 2 dx, dy = window_size.width(), window_size.height() display_geometry = QDesktopWidget().availableGeometry() display_center = display_geometry.center() x = display_center.x() - dx / 2 y = display_center.y() - dy / 2 self.setGeometry(int(x), int(y), dx, dy) self.setFixedSize(dx, dy) # Initialize statistic and exercise result variable. self.exercise = Exercise(num_shapes=2) self.statistics = pd.DataFrame(columns=["time", "is_correct", "pressed", "solution"]) # Set window properties. self.setWindowTitle('eligo_reactions') self.show()
def __init__(self, parent=None): super().__init__(parent, Qt.Dialog | Qt.WindowStaysOnTopHint) dGeometry = QDesktopWidget().screenGeometry() self.vl = QVBoxLayout(self) self.pBar = QProgressBar() self.status = QLabel() self.status.setObjectName('statusProgressLabel') self.setLayout(self.vl) self.vl.addItem( QSpacerItem(10, 50, QSizePolicy.Expanding, QSizePolicy.Expanding)) self.vl.addWidget(self.status, 0, Qt.AlignCenter) self.vl.addWidget(self.pBar, 0, Qt.AlignCenter) self.vl.addItem( QSpacerItem(10, 50, QSizePolicy.Expanding, QSizePolicy.Expanding)) self.setMaximumSize(dGeometry.width() / 2, dGeometry.height() / 3) center = dGeometry.center() if 0: self.move(center.x() - self.width(), center.y() - self.height())
def __init__(self): super().__init__() # ->> Tooltip preferences QToolTip.setFont(QFont("SansSerif", 10)) # ->> Centering monitor_resolution = QDesktopWidget().screenGeometry() widget_size = QSize(1280, 720) padding_point = QPoint( monitor_resolution.center().x() - widget_size.width() / 2, monitor_resolution.center().y() - widget_size.height() / 2) self.setGeometry(QRect(padding_point, widget_size)) # ->> Windows meta self.setWindowTitle("Foo Application") self.setWindowIcon(QIcon(r"resources/enak.jpg")) # ->> Buttons # btn = QPushButton(QIcon(r"C:\Users\\Pictures\x-png-15.png"), "Quit", self) # btn.setToolTip("button tooltip") # btn.move(widget_size.width() - 100, 30) # btn.resize(btn.sizeHint()) # btn.clicked.connect(self.teardown) # ->> Status Bar self.statusBar().showMessage("hey Ron") # ->> Menu Bar bar = self.menuBar() bar.setNativeMenuBar(False) menu_file = bar.addMenu("&파일") menu_help = bar.addMenu("&도움말") # # ->> Actions: File action_new_project = QAction("프로젝트 생성", self) action_new_project.setShortcut("CTRL+SHIFT+N") action_new_project.setStatusTip("Create new project") menu_file.addAction(action_new_project) action_open_project = QAction("프로젝트 열기", self) action_open_project.setShortcut("CTRL+SHIFT+O") action_open_project.setStatusTip("Open project") menu_file.addAction(action_open_project) menu_file.addSeparator() action_exit = QAction("Exit", self) action_exit.setShortcut("CTRL+Q") action_exit.setStatusTip("Exit application") action_exit.triggered.connect(self.teardown) menu_file.addAction(action_exit) # # ->> Actions: Help action_help_program_info = QAction("프로그램 정보", self) action_help_program_info.setStatusTip("프로그램의 정보를 확인합니다.") action_help_program_info.triggered.connect(lambda: help.UIProgramInfo().showModal()) menu_help.addAction(action_help_program_info) # ->> Layouts # # ->> Left menu # # # ->> Project info label_project_indi = QLabel("현재 프로젝트") label_project_name = QLabel("> Foo project.sweatproject") left_project_info = QVBoxLayout() left_project_info.addWidget(label_project_indi) left_project_info.addWidget(label_project_name) # # # ->> Scroll label_scroll_desc = QLabel("등록된 감시 폴더 경로") left_scroll = QScrollArea() scroll_widget = QWidget() scroll_grid = QVBoxLayout(scroll_widget) left_scroll.setMaximumWidth(230) scroll_grid.setAlignment(QtCore.Qt.AlignTop) left_scroll.setWidgetResizable(True) for n in range(40): content = QLabel("Content {}".format(n + 1)) content.mousePressEvent = functools.partial(self.file_path_label_clicked, content) scroll_grid.addWidget(content) left_scroll.setWidget(scroll_widget) left_bottom = QVBoxLayout() left_bottom.addWidget(label_scroll_desc) left_bottom.addWidget(left_scroll) left_menu_layout = QVBoxLayout() left_menu_layout.addLayout(left_project_info) left_menu_layout.addWidget(QLabel("")) # Spacer left_menu_layout.addLayout(left_bottom) left_menu = QWidget() left_menu.setLayout(left_menu_layout) left_menu.setMaximumWidth(230) # # -> Right menu # # # ->> Directory info self.label_directory_name = QLabel() font = self.label_directory_name.font() font.setPointSize(13) font.setBold(True) font.setWeight(75) self.label_directory_name.setFont(font) self.label_directory_name.setText("Directory name here") right_menu_layout = QVBoxLayout() right_menu_layout.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) right_menu_layout.addWidget(self.label_directory_name) right_menu = QWidget() right_menu.setLayout(right_menu_layout) layout = QHBoxLayout() layout.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) layout.addWidget(left_menu) layout.addWidget(right_menu) central = QWidget(self) central.setLayout(layout) self.setCentralWidget(central)
def __init__(self, *args, **kwargs): super(FrameLessWindow, self).__init__(*args, **kwargs) # self.mousePressed = False # 设置窗体的图标和名称 self.setWindowIcon(QIcon("media/logo.png")) self.setWindowTitle("瑞达期货研究院分析决策系统") # 标题栏 self.title_bar = TitleBar(parent=self) # 导航栏 self.navigation_bar = NavigationBar(parent=self) # 导航栏的信号 self.navigation_bar.clicked_login_button.connect(self.user_to_login) self.navigation_bar.clicked_register_button.connect( self.user_to_register) self.navigation_bar.clicked_logout_button.connect(self.user_to_logout) self.navigation_bar.module_bar.menu_clicked.connect( self.accessed_module) # 选择了某个模块的 self.navigation_bar.permit_bar.to_usercenter.connect( self.skip_to_usercenter) # 跳转至用户中心 # 窗口承载体 self.page_container = LoadedPage(parent=self) # 属性、样式 user_desktop = QDesktopWidget().availableGeometry( ) # 用户的桌面信息,来改变自身窗体大小 max_width = user_desktop.width() max_height = user_desktop.height() self.resize(max_width * 0.75, max_height * 0.8) self.setMaximumSize(max_width, max_height) # 最大为用户桌面大小 self.setMinimumSize(max_width * 0.5, max_height * 0.5) # 最小为用户桌面大小的一半 my_frame = self.frameGeometry() # 1 (三步法放置桌面中心)自身窗体信息(虚拟框架) my_frame.moveCenter(user_desktop.center()) # 2 框架中心移动到用户桌面中心 self.move(my_frame.topLeft()) # 3 窗口左上角与虚拟框架左上角对齐 self.setWindowFlags(Qt.FramelessWindowHint) # 无边框 self.setAttribute(Qt.WA_TranslucentBackground, True) # 背景全透明(影响子窗口) self._pressed = False self._direction = None self._mouse_pos = None self.setMouseTracking(True) # 鼠标不点下移动依然有效(针对本窗口, 子控件无效) self.title_bar.installEventFilter(self) # 子控件安装事件事件过滤 self.navigation_bar.installEventFilter(self) self.page_container.installEventFilter(self) # 布局 layout = QVBoxLayout(margin=self.MARGIN, spacing=0) layout.addWidget(self.title_bar) layout.addWidget(self.navigation_bar) layout.addWidget(self.page_container) self.setLayout(layout) if settings.cache_dawn.value("updated") != "0": self.update_tips = UpdateTipsLabel(self) self.update_tips.setWordWrap(True) self.update_tips.setWindowFlags(Qt.Dialog) self.update_tips.setWindowTitle("【1.3.1更新】") self.update_tips.setFixedSize(250, 150) self.update_tips.setText("<p>【1.3.1】版本更新:</p>" "<p>1 新增交易所数据查询</p>" "<p>2 新增品种净持仓查询</p>" "<p>3 数据查询结果支持Ctrl+C快捷键复制连续的数据块</p>") tips_frame = self.update_tips.frameGeometry() tips_frame.moveCenter(user_desktop.center()) self.update_tips.move(tips_frame.topLeft()) self.update_tips.show() self.update_tips.setStyleSheet( "color:rgb(7,99,109);font-weight:bold;font-size:14px;padding-left:30px" )
def move_center(self): m_rect = self.frameGeometry() w_rect = QDesktopWidget().availableGeometry() w_center_point = w_rect.center() m_rect.moveCenter(w_center_point) self.move(m_rect.topLeft())
def center(self): dec=QDesktopWidget().geometry() win=self.geometry() win.moveCenter(dec.center())