You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt5按钮布局与菜单功能异常问题求助

Fixing PyQt5 Button and Menu Issues

Hey there! Let's break down and fix the two issues you're facing with PyQt5 one by one.

1. Button Size & Position Problems

What's Going Wrong?

  • When using a layout like QGridLayout, the layout takes full control of your widget's size and position. Calls to resize(), setGeometry(), or even adjustSize() will be ignored because the layout overrides these settings.
  • Your addWidget call has invalid parameters: layout.addWidget(Import_Deck, 0, 0, 0, -100). The last two arguments are row span and column span—using 0 or negative values breaks the layout's calculation logic.

Fix Steps

  • Use setFixedSize() instead of resize() to lock the button to your desired dimensions (this tells the layout to respect the size).
  • Pass valid span values to addWidget. For example, if you want the button to span multiple columns in row 0, use something like 1, 3 (1 row span, 3 column span).

2. Menu Display & AttributeError Issues

What's Going Wrong?

  • AttributeError: QWidget doesn't have a menuBar() method—this is a built-in feature of QMainWindow, which is designed for standard window layouts with menus, toolbars, etc.
  • QToolButton Menu Not Showing: The code snippet you provided doesn't create a QMenu object before calling setMenu(menu)—you need to define the menu and add items to it first.

Fix Steps

  • Change your Window class to inherit from QMainWindow instead of QWidget. This gives you access to menuBar() and proper menu handling.
  • Create a QMenu instance, add menu items to it, then attach it to your QToolButton.

Full Fixed Code

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setGeometry(0, 0, 2000, 992)
        self.setWindowTitle("MTGCalc")
        
        # Create central widget and layout (required for QMainWindow)
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QGridLayout(central_widget)
        
        # Fixed-size Import Deck button
        Import_Deck = QPushButton("Import Deck", self)
        Import_Deck.setFixedSize(150, 50)  # Lock size instead of resize
        layout.addWidget(Import_Deck, 0, 0, 1, 2)  # Valid row/column span
        
        # Add QToolButton with working menu
        formatslider = QToolButton()
        formatslider.setText("Formats")
        
        # Create menu and add items
        menu = QMenu()
        menu.addAction("Standard")
        menu.addAction("Modern")
        menu.addAction("Commander")
        
        formatslider.setMenu(menu)
        formatslider.setPopupMode(QToolButton.MenuButtonPopup)
        layout.addWidget(formatslider, 0, 2)  # Place in column 2
        
        self.mainmenufunc()

    def mainmenufunc(self):
        # Now menuBar() works since we inherit QMainWindow
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('Decklist')
        editMenu = mainMenu.addMenu('Probability')
        viewMenu = mainMenu.addMenu('Recommended Mana')

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())

Key Changes Explained

  • Switched from QWidget to QMainWindow and added a central widget (required for QMainWindow to hold layouts).
  • Used setFixedSize() for the Import button to enforce dimensions.
  • Fixed the addWidget span parameters to valid values.
  • Created a proper QMenu with items for the QToolButton.
  • Removed redundant imports and the invalid _main_() call at the end.

内容的提问来源于stack exchange,提问作者zettai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:07:31