• <legend id='dZM7H'><style id='dZM7H'><dir id='dZM7H'><q id='dZM7H'></q></dir></style></legend>

    1. <tfoot id='dZM7H'></tfoot>

        <small id='dZM7H'></small><noframes id='dZM7H'>

        <i id='dZM7H'><tr id='dZM7H'><dt id='dZM7H'><q id='dZM7H'><span id='dZM7H'><b id='dZM7H'><form id='dZM7H'><ins id='dZM7H'></ins><ul id='dZM7H'></ul><sub id='dZM7H'></sub></form><legend id='dZM7H'></legend><bdo id='dZM7H'><pre id='dZM7H'><center id='dZM7H'></center></pre></bdo></b><th id='dZM7H'></th></span></q></dt></tr></i><div id='dZM7H'><tfoot id='dZM7H'></tfoot><dl id='dZM7H'><fieldset id='dZM7H'></fieldset></dl></div>
          <bdo id='dZM7H'></bdo><ul id='dZM7H'></ul>

        PyQt:如何创建自定义组合标题栏和菜单栏

        时间:2024-08-11
        <legend id='rJVEU'><style id='rJVEU'><dir id='rJVEU'><q id='rJVEU'></q></dir></style></legend>

        • <bdo id='rJVEU'></bdo><ul id='rJVEU'></ul>
            <tfoot id='rJVEU'></tfoot>
                <i id='rJVEU'><tr id='rJVEU'><dt id='rJVEU'><q id='rJVEU'><span id='rJVEU'><b id='rJVEU'><form id='rJVEU'><ins id='rJVEU'></ins><ul id='rJVEU'></ul><sub id='rJVEU'></sub></form><legend id='rJVEU'></legend><bdo id='rJVEU'><pre id='rJVEU'><center id='rJVEU'></center></pre></bdo></b><th id='rJVEU'></th></span></q></dt></tr></i><div id='rJVEU'><tfoot id='rJVEU'></tfoot><dl id='rJVEU'><fieldset id='rJVEU'></fieldset></dl></div>

                  <tbody id='rJVEU'></tbody>

              • <small id='rJVEU'></small><noframes id='rJVEU'>

                1. 本文介绍了PyQt:如何创建自定义组合标题栏和菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将菜单和标题设置在一个栏中,但不知道如何布局菜单栏和标题栏(或我自己的标题栏)。

                  # -*- coding:utf-8 -*- 
                  
                  from PyQt5 import QtWidgets,QtGui,QtCore
                  import sys
                  
                  qss = ""
                  
                  class UI(QtWidgets.QMainWindow):
                      def __init__(self):
                          super().__init__()
                          self.setui()  
                      def setui(self):
                          #----------main-window----------------------
                          self.setGeometry(0,0,1366,768) #x,y,w,h
                          self.setWindowTitle('hello world')
                          self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
                          #----------menu-bar---------------------
                          #--------file-menu-----
                          self.menu_file=self.menuBar().addMenu('file')
                          self.menu_file_open=self.menu_file.addAction('open')
                          self.menu_file_save=self.menu_file.addAction('save')
                          self.menu_file_saveas=self.menu_file.addAction('save as...')
                          self.menu_file_quit=self.menu_file.addAction('exit')
                          #-----------experient-menu----------
                          self.menu_work=self.menuBar().addMenu('work')
                          #-------------analysis-menu---------
                          self.menu_analysis=self.menuBar().addMenu('analysis')
                          #------------edit-menu--------------
                          self.menu_edit=self.menuBar().addMenu('edit')
                          #------------window-menu--------------
                          self.menu_window=self.menuBar().addMenu('window')
                          #------------help---menu--------------
                          self.menu_help=self.menuBar().addMenu('help')
                          #-------------set---qss----------------------
                          self.setStyleSheet(qss)
                          #-------functions--connect-------------------
                          self.menu_file_quit.triggered.connect(QtWidgets.qApp.quit)
                          self.show()
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      ex = UI()
                      sys.exit(app.exec_()) 
                  

                  我希望栏包括图标、菜单、标题和这三个按钮,就像vscode的菜单栏一样。

                  推荐答案

                  试用:

                  import sys
                  from PyQt5.QtCore import pyqtSlot, QPoint, Qt, QRect
                  from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, QHBoxLayout,
                                               QVBoxLayout, QTabWidget, QWidget, QAction,
                                               QLabel, QSizeGrip, QMenuBar, qApp)
                  from PyQt5.QtGui import QIcon
                  
                  
                  class TitleBar(QWidget):
                      height = 35
                      def __init__(self, parent):
                          super(TitleBar, self).__init__()
                          self.parent = parent
                          self.layout = QHBoxLayout()
                          self.layout.setContentsMargins(0,0,0,0)
                  
                          self.menu_bar = QMenuBar()
                          self.menu_bar.setStyleSheet("""
                              color: #fff;
                              background-color: #23272A;
                              font-size: 14px;
                              padding: 4px; 
                          """)  
                          self.menu_file = self.menu_bar.addMenu('file')
                          self.menu_file_open=self.menu_file.addAction('open')
                          self.menu_file_save=self.menu_file.addAction('save')
                          self.menu_file_saveas=self.menu_file.addAction('save as...')
                          self.menu_file_quit=self.menu_file.addAction('exit')
                          self.menu_file_quit.triggered.connect(qApp.quit)
                  
                          self.menu_work=self.menu_bar.addMenu('work')
                          self.menu_analysis=self.menu_bar.addMenu('analysis')
                          self.menu_edit=self.menu_bar.addMenu('edit')
                          self.menu_window=self.menu_bar.addMenu('window')
                          self.menu_help=self.menu_bar.addMenu('help')
                  
                          self.layout.addWidget(self.menu_bar) 
                  
                          self.title = QLabel("Hello World!")
                          self.title.setFixedHeight(self.height)
                          self.layout.addWidget(self.title)
                          self.title.setStyleSheet("""
                              background-color: #23272a;  /* 23272a   #f00*/
                              font-weight: bold;
                              font-size: 16px;
                              color: blue;
                              padding-left: 170px;
                          """)
                  
                          self.closeButton = QPushButton(' ')                            
                          self.closeButton.clicked.connect(self.on_click_close)
                          self.closeButton.setStyleSheet("""
                              background-color: #DC143C;
                              border-radius: 10px;
                              height: {};
                              width: {};
                              margin-right: 3px;
                              font-weight: bold;
                              color: #000;
                              font-family: "Webdings";
                              qproperty-text: "r";
                          """.format(self.height/1.7,self.height/1.7))
                  
                          self.maxButton = QPushButton(' ')
                          self.maxButton.clicked.connect(self.on_click_maximize)
                          self.maxButton.setStyleSheet("""
                              background-color: #32CD32;
                              border-radius: 10px;
                              height: {};
                              width: {};
                              margin-right: 3px;
                              font-weight: bold;
                              color: #000;
                              font-family: "Webdings";
                              qproperty-text: "1";
                          """.format(self.height/1.7,self.height/1.7))
                  
                          self.hideButton = QPushButton(' ')
                          self.hideButton.clicked.connect(self.on_click_hide)
                          self.hideButton.setStyleSheet("""
                              background-color: #FFFF00;
                              border-radius: 10px;
                              height: {};
                              width: {};
                              margin-right: 3px;
                              font-weight: bold;
                              color: #000;
                              font-family: "Webdings";
                              qproperty-text: "0";
                          """.format(self.height/1.7,self.height/1.7))
                  
                          self.layout.addWidget(self.hideButton)
                          self.layout.addWidget(self.maxButton)
                          self.layout.addWidget(self.closeButton)
                          self.setLayout(self.layout)
                  
                          self.start = QPoint(0, 0)
                          self.pressing = False
                          self.maximaze = False
                  
                      def resizeEvent(self, QResizeEvent):
                          super(TitleBar, self).resizeEvent(QResizeEvent)
                          self.title.setFixedWidth(self.parent.width())
                  
                      def mousePressEvent(self, event):
                          self.start = self.mapToGlobal(event.pos())
                          self.pressing = True
                  
                      def mouseMoveEvent(self, event):
                          if self.pressing:
                              self.end = self.mapToGlobal(event.pos())
                              self.movement = self.end-self.start
                              self.parent.move(self.mapToGlobal(self.movement))
                              self.start = self.end
                  
                      def mouseReleaseEvent(self, QMouseEvent):
                          self.pressing = False
                  
                      def on_click_close(self):
                          sys.exit()
                  
                      def on_click_maximize(self):
                          self.maximaze = not self.maximaze
                          if self.maximaze:    self.parent.setWindowState(Qt.WindowNoState)
                          if not self.maximaze:
                              self.parent.setWindowState(Qt.WindowMaximized)
                  
                      def on_click_hide(self):
                          self.parent.showMinimized()
                  
                  
                  class StatusBar(QWidget):
                      def __init__(self, parent):
                          super(StatusBar, self).__init__()
                          self.initUI()
                          self.showMessage("showMessage: Hello world!")
                  
                      def initUI(self):
                          self.label = QLabel("Status bar...")
                          self.label.setFixedHeight(24)
                          self.label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
                          self.label.setStyleSheet("""
                              background-color: #23272a;
                              font-size: 12px;
                              padding-left: 5px;
                              color: white;
                          """)
                          self.layout = QHBoxLayout()
                          self.layout.setContentsMargins(0,0,0,0)
                          self.layout.addWidget(self.label)
                          self.setLayout(self.layout)
                  
                      def showMessage(self, text):
                          self.label.setText(text)
                  
                  
                  class MainWindow(QWidget):
                      def __init__(self):
                          super(MainWindow, self).__init__()
                          self.setFixedSize(800, 400)
                          self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)        
                          self.setStyleSheet("background-color: #2c2f33;")
                          self.setWindowTitle('Code Maker')
                  
                          self.title_bar = TitleBar(self) 
                          self.status_bar = StatusBar(self)
                  
                          self.layout  = QVBoxLayout()
                          self.layout.setContentsMargins(0,0,0,0)
                          self.layout.addWidget(self.title_bar)
                          self.layout.addStretch(1)
                          self.layout.addWidget(self.status_bar)
                          self.layout.setSpacing(0)                               
                          self.setLayout(self.layout)
                  
                  
                  if __name__ == "__main__":
                      app = QApplication(sys.argv)
                      mw = MainWindow()
                      mw.show()
                      sys.exit(app.exec_())
                  

                  这篇关于PyQt:如何创建自定义组合标题栏和菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:触摸板(PyTorch)ADD_GRAPH中出错 下一篇:将小部件旋转一定程度

                  相关文章

                  <i id='3SQ02'><tr id='3SQ02'><dt id='3SQ02'><q id='3SQ02'><span id='3SQ02'><b id='3SQ02'><form id='3SQ02'><ins id='3SQ02'></ins><ul id='3SQ02'></ul><sub id='3SQ02'></sub></form><legend id='3SQ02'></legend><bdo id='3SQ02'><pre id='3SQ02'><center id='3SQ02'></center></pre></bdo></b><th id='3SQ02'></th></span></q></dt></tr></i><div id='3SQ02'><tfoot id='3SQ02'></tfoot><dl id='3SQ02'><fieldset id='3SQ02'></fieldset></dl></div>

                    <small id='3SQ02'></small><noframes id='3SQ02'>

                    • <bdo id='3SQ02'></bdo><ul id='3SQ02'></ul>
                    <tfoot id='3SQ02'></tfoot>

                    1. <legend id='3SQ02'><style id='3SQ02'><dir id='3SQ02'><q id='3SQ02'></q></dir></style></legend>