按钮未显示在PYQT QVBoxLayout上。

huangapple go评论54阅读模式
英文:

Buttons not appearing on PYQT QVBoxLayout

问题

我正在尝试创建一个简单的应用程序,其中有5个功能,每个功能都可以从主页上的按钮访问。

App类的代码如下:

import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication

class App(QtWidgets.QMainWindow):
    
    def __init__(self):
        super().__init__()
        self._main = QtWidgets.QWidget()
        self.initUI_M()

    def initUI_M(self):
        self.setWindowTitle("QC Tool")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

        layout = QtWidgets.QVBoxLayout()

        button1 = QtWidgets.QPushButton("GPS")
        button1.clicked.connect(partial(self.plotFlight_map))
        layout.addWidget(button1)

        button2 = QtWidgets.QPushButton("XY")
        button2.clicked.connect(partial(self.plot_xy))
        layout.addWidget(button2)

        button3 = QtWidgets.QPushButton("Accel")
        button3.clicked connect(partial(self.plot_accel))
        layout.addWidget(button3)

        button4 = QtWidgets.QPushButton("Gyro")
        button4.clicked.connect(partial(self.plot_gyro))
        layout.addWidget(button4)

        button5 = QtWidgets.QPushButton("Altimeter")
        button5.clicked.connect(partial(self.plot_Altimeter))
        layout.addWidget(button5)

        self._main.setLayout(layout)

        self.center()
        self.show()

在运行时,我得到以下结果...

按钮未显示在PYQT QVBoxLayout上。

我尝试过在按钮和QVBoxLayout的构造中将self._main或仅self设置为父对象,并尝试不将方法分配给按钮。所有与clicked.connect连接的方法都存在并先前连接到了工作的菜单。

我甚至尝试过不创建小部件对象,只是将布局添加到self。

无论我尝试什么,都得到完全相同的输出。

我知道这是一个语法问题,但我找不到它...

英文:

I'm trying to create a simple app with 5 functions each accessed from a button on the main page.

The App class looks like this

import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication

class App(QtWidgets.QMainWindow):

	def __init__(self):
		super().__init__()
		self._main = QtWidgets.QWidget()
		self.initUI_M()

	def initUI_M(self):
		self.setWindowTitle("QC Tool")
		self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

		layout =QtWidgets.QVBoxLayout( )

		button1 =  QtWidgets.QPushButton("GPS")
		button1.clicked.connect(partial(self.plotFlight_map))
		layout.addWidget(button1)

		button2 =  QtWidgets.QPushButton("XY")
		button2.clicked.connect(partial(self.plot_xy))
		layout.addWidget(button2)

		button3 =  QtWidgets.QPushButton("Accel")
		button3.clicked.connect(partial(self.plot_accel))
		layout.addWidget(button3)

		button4 =  QtWidgets.QPushButton("Gyro")
		button4.clicked.connect(partial(self.plot_gyro))
		layout.addWidget(button4)

		button5 =  QtWidgets.QPushButton("Altimeter")
		button5.clicked.connect(partial(self.plot_Altimeter))
		layout.addWidget(button5)

		self._main.setLayout(layout)

		self.center()
		self.show()

This is what I get on running...
按钮未显示在PYQT QVBoxLayout上。

I've tried setting self._main, or just self as the parent on construction of the buttons and the QVBoxLayout, and I've tried not assigning methods to the buttons. All of the methods connected to the clicked.connect exist and were previously connected to a menu that worked.

I've even tried not creating the widget object and just adding the layout to self.

I get the exact same output no matter what I try.

I know it's a syntax issue, but I can't find it...

答案1

得分: 1

你忘记设置QMainWindow的主窗口部件。你已经创建了一个名为"_main"的QWidget对象,但你仍然需要将它设置为主窗口的中央部件。

你需要使用QMainWindow的setCentralWidget方法来设置主窗口的主部件。

def initUI_M(self):
    self.setWindowTitle("QC Tool")
    self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

    central_widget = QtWidgets.QWidget()  # 在你的代码中添加这行
    self.setCentralWidget(central_widget)  # 在你的代码中添加这行

    layout = QtWidgets.QVBoxLayout(central_widget)

然后你只需要添加以下函数

```python
    self.center()
    self.show()

def plotFlight_map(self):  # 在你的代码中添加这行
    print("GPS")

def plot_xy(self):  # 在你的代码中添加这行
    print("XY")

def plot_accel(self):  # 在你的代码中添加这行
    print("Accel")

def plot_gyro(self):  # 在你的代码中添加这行
    print("Gyro")

def plot_Altimeter(self):  # 在你的代码中添加这行
    print("Altimeter")

def center(self):  # 在你的代码中添加这行
    qr = self.frameGeometry()
    cp = QtWidgets.QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())

if __name__ == '__main__':  # 在你的代码中添加这行
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
英文:

You forgot to set the main widget of the QMainWindow. You've created a QWidget object called "_main", but you still need to set it as the central widget of your main window.

You need to set the main widget using the setCentralWidget method of QMainWindow.

def initUI_M(self):
    self.setWindowTitle("QC Tool")
    self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

    central_widget = QtWidgets.QWidget()  # Add This to your code
    self.setCentralWidget(central_widget) # Add This to your code

    layout = QtWidgets.QVBoxLayout(central_widget)

Then all you need to do is add the following functions:

    self.center()
    self.show()
    
    def plotFlight_map(self): # Add This to your code
        print("GPS")
    
    def plot_xy(self): # Add This to your code
        print("XY")
    
    def plot_accel(self): # Add This to your code
        print("Accel")
    
    def plot_gyro(self): # Add This to your code
        print("Gyro")
    
    def plot_Altimeter(self): # Add This to your code
        print("Altimeter")
    
    def center(self): # Add This to your code
        qr = self.frameGeometry()
        cp = QtWidgets.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == '__main__': # Add This to your code
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

huangapple
  • 本文由 发表于 2023年5月15日 09:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定