PyQt5按钮布局与菜单功能异常问题求助
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 toresize(),setGeometry(), or evenadjustSize()will be ignored because the layout overrides these settings. - Your
addWidgetcall 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 ofresize()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 like1, 3(1 row span, 3 column span).
2. Menu Display & AttributeError Issues
What's Going Wrong?
- AttributeError:
QWidgetdoesn't have amenuBar()method—this is a built-in feature ofQMainWindow, which is designed for standard window layouts with menus, toolbars, etc. - QToolButton Menu Not Showing: The code snippet you provided doesn't create a
QMenuobject before callingsetMenu(menu)—you need to define the menu and add items to it first.
Fix Steps
- Change your
Windowclass to inherit fromQMainWindowinstead ofQWidget. This gives you access tomenuBar()and proper menu handling. - Create a
QMenuinstance, add menu items to it, then attach it to yourQToolButton.
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
QWidgettoQMainWindowand added a central widget (required for QMainWindow to hold layouts). - Used
setFixedSize()for the Import button to enforce dimensions. - Fixed the
addWidgetspan parameters to valid values. - Created a proper
QMenuwith items for theQToolButton. - Removed redundant imports and the invalid
_main_()call at the end.
内容的提问来源于stack exchange,提问作者zettai
相关产品推荐
相关产品推荐

