Python 如果窗口/工具栏大小/宽度减小,请将QpushButton放入下拉菜单

Python 如果窗口/工具栏大小/宽度减小,请将QpushButton放入下拉菜单,python,python-3.x,linux,pyside2,qtoolbar,Python,Python 3.x,Linux,Pyside2,Qtoolbar,我有一个QPushButtons按钮的QToolBar。当调整窗口的大小以减小其宽度时,工具栏的宽度将减小。如果宽度减小到超过某一点,则按钮开始消失。我认为情况是可以理解的 在某些情况下,必须减小窗口宽度,因此设置最小宽度是不可能的 因此,基本上,无论窗口大小如何,所有这些按钮都应该可以访问 我想把这些按钮放到一个下拉菜单中,因为寡妇的尺寸正在缩小。还有别的办法吗?我甚至不知道从哪里开始。如果有人能告诉我怎么做或者给我指出正确的方向,那就太好了 import sys from PySide2.

我有一个QPushButtons按钮的QToolBar。当调整窗口的大小以减小其宽度时,工具栏的宽度将减小。如果宽度减小到超过某一点,则按钮开始消失。我认为情况是可以理解的

在某些情况下,必须减小窗口宽度,因此设置最小宽度是不可能的

因此,基本上,无论窗口大小如何,所有这些按钮都应该可以访问

我想把这些按钮放到一个下拉菜单中,因为寡妇的尺寸正在缩小。还有别的办法吗?我甚至不知道从哪里开始。如果有人能告诉我怎么做或者给我指出正确的方向,那就太好了

import sys

from PySide2.QtWidgets import *
from PySide2 import *

class MainWindow(QWidget):
   def __init__(self):
       super(MainWindow, self).__init__()
       self.resize(500, 400)
       self.setMinimumSize(200, 200)

       toolbar = QToolBar()
       toolbar.setStyleSheet("QToolBar{"
                          "background: rgb(60, 60, 60);}")
       toolbar.setFixedHeight(30)

      a = QPushButton()
      a.setFixedSize(25, 25)
      a.setText("a")
      a.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      b = QPushButton()
      b.setFixedSize(25, 25)
      b.setText("b")
      b.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      c = QPushButton()
      c.setFixedSize(25, 25)
      c.setText("c")
      c.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      d = QPushButton()
      d.setFixedSize(25, 25)
      d.setText("d")
      d.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      e = QPushButton()
      e.setFixedSize(25, 25)
      e.setText("e")
      e.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      f = QPushButton()
      f.setFixedSize(25, 25)
      f.setText("f")
      f.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      g = QPushButton()
      g.setFixedSize(25, 25)
      g.setText("g")
      g.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      h = QPushButton()
      h.setFixedSize(25, 25)
      h.setText("h")
      h.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      i = QPushButton()
      i.setFixedSize(25, 25)
      i.setText("i")
      i.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")
      j = QPushButton()
      j.setFixedSize(25, 25)
      j.setText("j")
      j.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow}")
      k = QPushButton()
      k.setFixedSize(25, 25)
      k.setText("k")
      k.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")

      l = QPushButton()
      l.setFixedSize(25, 25)
      l.setText("l")
      l.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")

      m = QPushButton()
      m.setFixedSize(25, 25)
      m.setText("m")
      m.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")

      n = QPushButton()
      n.setFixedSize(25, 25)
      n.setText("n")
      n.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")

      o = QPushButton()
      o.setFixedSize(25, 25)
      o.setText("o")
      o.setStyleSheet("QPushButton{"
                    "color: red;"
                    "background: yellow;}")

      toolbar.addWidget(a)
      toolbar.addWidget(b)
      toolbar.addWidget(c)
      toolbar.addWidget(d)
      toolbar.addWidget(e)
      toolbar.addWidget(f)
      toolbar.addWidget(g)
      toolbar.addWidget(h)
      toolbar.addWidget(i)
      toolbar.addWidget(j)
      toolbar.addWidget(k)
      toolbar.addWidget(l)
      toolbar.addWidget(m)
      toolbar.addWidget(n)
      toolbar.addWidget(o)

      #solution

      toolbar.addAction("m", "m")
      toolbar.addAction("w", "w")
      toolbar.addAction("q", "q")
      toolbar.setStyleSheet("QToolBar{background: grey;}"
                          "QToolButton { background:yellow ;"
                          "color: red;}")          


      layout = QVBoxLayout()
      layout.addWidget(toolbar)
      self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

我没有使用QPushButtons,而是向QToolBar添加了以下操作:

toolbar.addAction("m", "m")
toolbar.addAction("w", "w")
toolbar.addAction("q", "q")
toolbar.setStyleSheet("QToolBar{background:grey;}"
                          "QToolButton {background:yellow ;"
                          "color: red;}")

请根据所选样式提供一个QToolBar应提供一些访问按钮的方法,这些按钮由于工具栏的大小调整而变得不可见。@eyllanesc我在Qt文档中添加了一些代码…当工具栏的大小调整到无法显示其包含的所有项时,扩展按钮将作为工具栏中的最后一项出现。按下扩展按钮将弹出一个菜单,其中包含当前不适合工具栏的项目。这可能只适用于操作,而不适用于小部件。我强烈建议使用操作,而不是将按钮添加为小部件。请注意,您可以将样式表应用于工具栏,而不是将QPushButton替换为表示每个操作的QToolButton。这是一个很好的解决方案指针。谢谢@ypnos。