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

PyQT6中grid.addWidget报TypeError错误的技术求助

PyQt6 QComboBox选中内容存变量时的TypeError问题解决

问题描述

尝试用PyQt6的QComboBox将选中内容存入变量以便后续调用,但运行时触发TypeError,提示arguments did not match any overloaded call。经排查问题出在grid.addWidget(lblPStyleR, 3, 0)这行代码,直接传入变量self.tstPTypeR也会触发相同错误,调整super().__init__语句无效。

错误栈信息:

Traceback (most recent call last):
  File "e:\python_projects\pac_org\main2.py", line 115, in <module>
    main()
  File "e:\python_projects\pac_org\main2.py", line 111, in main
    ex = MainWindow()
  File "e:\python_projects\pac_org\main2.py", line 9, in __init__
    self.initUI()
  File "e:\python_projects\pac_org\main2.py", line 74, in initUI
    grid.addWidget(lblPStyleR, 3, 0)
TypeError: arguments did not match any overloaded call:
  addWidget(self, QWidget): argument 1 has unexpected type 'str'
  addWidget(self, QWidget, int, int, alignment: Qt.AlignmentFlag = Qt.Alignment()): argument 1 has unexpected type 'str'
  addWidget(self, QWidget, int, int, int, int, alignment: Qt.AlignmentFlag = Qt.Alignment()): argument 1 has unexpected type 'str'

完整代码:

from PyQt6.QtWidgets import (QWidget, QLabel, QLineEdit, QGridLayout, QApplication, QComboBox)
import sys

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
    
        self.initUI()

    def initUI(self):
        self.tstPStyleR = str("0")
        self.tstPTypeR = str("0")
        #self.strPLoc = 0
        #self.strAIMVer = 0

        lblPSN = QLabel("PAC Serial Number: ")
        ledPSN = QLineEdit()
        
        lblPType = QLabel("PAC Type: ")
        cboPType = QComboBox(self)
        cboPType.addItems(["Single", "Double", "Triple"])
        cboPType.textActivated[str].connect(self.onPTypeActivate)
        self.lblPTypeR = QLabel("Single", self)

        lblPStyle = QLabel("PAC Style: ")
        cboPStyle = QComboBox(self)
        cboPStyle.addItems(["3x Fuse, 1x 48v", "3x Fuse, 2x 48v", "0x Fuse, 1x 48v", "0x Fuse, 2x 48v"])
        cboPStyle.textActivated[str].connect(self.onPStyleActivate)
        self.lblPStyle = QLabel("3x Fuse, 1x 48v", self)

        lblPLoc = QLabel("PAC Location: ")
        cboPLoc = QComboBox(self)
        cboPLoc.addItems(["PAC Cell", "PAC Cell: Repair", "Blue Line", "Frame"])
        cboPLoc.textActivated[str].connect(self.onPLocActivate)
        self.lblPLoc = QLabel("PAC Cell")

        lblAIMSN = QLabel("AIM SN: ")
        ledAIMSN = QLineEdit()

        lblAIMVer = QLabel("AIM Version: ")
        cboAIMVer = QComboBox(self)
        cboAIMVer.addItems(["GREEN 1.4", "RED 2.0", "RED 3.0"])
        cboAIMVer.textActivated[str].connect(self.onAIMVerActivate)
        self.label = QLabel("GREEN 1.4", self)

        grid = QGridLayout()
        grid.setSpacing(10)

        grid.addWidget(lblPSN, 1, 0)
        grid.addWidget(ledPSN, 1, 1)
        grid.addWidget(lblPType, 1, 2)
        grid.addWidget(cboPType, 1, 3)
        grid.addWidget(self.lblPTypeR, 1, 4)
        grid.addWidget(lblPStyle, 1, 5)
        grid.addWidget(cboPStyle, 1, 6)
        grid.addWidget(self.lblPStyle, 1, 7)
        grid.addWidget(lblPLoc, 1, 8)
        grid.addWidget(cboPLoc, 1, 9)
        grid.addWidget(self.lblPLoc, 1, 10)

        grid.addWidget(lblAIMSN, 2, 0)
        grid.addWidget(ledAIMSN, 2, 1)
        grid.addWidget(lblAIMVer, 2, 2)
        grid.addWidget(cboAIMVer, 2, 3)
        grid.addWidget(self.label, 2, 4)
        self.setLayout(grid)

        lblPStyleR = str(self.tstPStyleR)
        #lblPTypeR = self.strPTypeR
        #lblPLocR = self.strPLoc
        #lblAIMVerR = self.strAIMVer

        grid.addWidget(lblPStyleR, 3, 0)
        #grid.addWidget(self.strPTypeR, 3, 1)
        #grid.addWidget(lblPLocR, 3, 2)
        #grid.addWidget(lblAIMVerR, 3, 3)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle("Stratacache PAC Manager")
        self.show()
   
    def onPStyleActivate(self, text):
        self.tstPStyleR = text
        print(self.tstPStyle)
        self.lblPStyle.setText(text)
        self.lblPStyle.adjustSize()
        return self.tstPStyleR

    def onPTypeActivate(self, text):
        self.tstPTypeR = text
        print(self.tstPStyleR)
        self.lblPTypeR.setText(text)
        self.lblPTypeR.adjustSize()


    def onPLocActivate(self, text):
        self.strPLoc = text
        self.lblPLoc.setText(text)
        self.lblPLoc.adjustSize()
        return self.strPLoc

    def onAIMVerActivate(self, text):
        self.strAIMVer = text
        self.label.setText(text)
        self.label.adjustSize()
        return self.strAIMVer

def main():
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec())

if __name__ == '__main__':
    main()

问题分析

错误提示已明确原因:QGridLayout.addWidget()的第一个参数必须是QWidget类型的控件对象,但你传入的lblPStyleR是字符串类型(self.tstPStyleR也是字符串),不符合方法的参数要求,因此触发TypeError。

另外代码中还有一处笔误:onPStyleActivate方法里打印的self.tstPStyle是未定义变量,应改为self.tstPStyleR,否则会触发NameError。

解决方案

  1. 要显示字符串变量的内容,必须先创建QLabel控件,将字符串设置为该控件的文本,再将QLabel添加到布局中。
  2. 修正onPStyleActivate方法中的打印变量名。

修改后的完整代码

from PyQt6.QtWidgets import (QWidget, QLabel, QLineEdit, QGridLayout, QApplication, QComboBox)
import sys

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
    
        self.initUI()

    def initUI(self):
        self.tstPStyleR = str("0")
        self.tstPTypeR = str("0")
        self.strPLoc = "PAC Cell"
        self.strAIMVer = "GREEN 1.4"

        lblPSN = QLabel("PAC Serial Number: ")
        ledPSN = QLineEdit()
        
        lblPType = QLabel("PAC Type: ")
        cboPType = QComboBox(self)
        cboPType.addItems(["Single", "Double", "Triple"])
        cboPType.textActivated[str].connect(self.onPTypeActivate)
        self.lblPTypeR = QLabel("Single", self)

        lblPStyle = QLabel("PAC Style: ")
        cboPStyle = QComboBox(self)
        cboPStyle.addItems(["3x Fuse, 1x 48v", "3x Fuse, 2x 48v", "0x Fuse, 1x 48v", "0x Fuse, 2x 48v"])
        cboPStyle.textActivated[str].connect(self.onPStyleActivate)
        self.lblPStyle = QLabel("3x Fuse, 1x 48v", self)

        lblPLoc = QLabel("PAC Location: ")
        cboPLoc = QComboBox(self)
        cboPLoc.addItems(["PAC Cell", "PAC Cell: Repair", "Blue Line", "Frame"])
        cboPLoc.textActivated[str].connect(self.onPLocActivate)
        self.lblPLoc = QLabel("PAC Cell", self)

        lblAIMSN = QLabel("AIM SN: ")
        ledAIMSN = QLineEdit()

        lblAIMVer = QLabel("AIM Version: ")
        cboAIMVer = QComboBox(self)
        cboAIMVer.addItems(["GREEN 1.4", "RED 2.0", "RED 3.0"])
        cboAIMVer.textActivated[str].connect(self.onAIMVerActivate)
        self.label = QLabel("GREEN 1.4", self)

        grid = QGridLayout()
        grid.setSpacing(10)

        grid.addWidget(lblPSN, 1, 0)
        grid.addWidget(ledPSN, 1, 1)
        grid.addWidget(lblPType, 1, 2)
        grid.addWidget(cboPType, 1, 3)
        grid.addWidget(self.lblPTypeR, 1, 4)
        grid.addWidget(lblPStyle, 1, 5)
        grid.addWidget(cboPStyle, 1, 6)
        grid.addWidget(self.lblPStyle, 1, 7)
        grid.addWidget(lblPLoc, 1, 8)
        grid.addWidget(cboPLoc, 1, 9)
        grid.addWidget(self.lblPLoc, 1, 10)

        grid.addWidget(lblAIMSN, 2, 0)
        grid.addWidget(ledAIMSN, 2, 1)
        grid.addWidget(lblAIMVer, 2, 2)
        grid.addWidget(cboAIMVer, 2, 3)
        grid.addWidget(self.label, 2, 4)

        # 创建用于显示变量的QLabel
        self.lblPStyleR_display = QLabel(self.tstPStyleR, self)
        self.lblPTypeR_display = QLabel(self.tstPTypeR, self)
        self.lblPLocR_display = QLabel(self.strPLoc, self)
        self.lblAIMVerR_display = QLabel(self.strAIMVer, self)

        grid.addWidget(self.lblPStyleR_display, 3, 0)
        grid.addWidget(self.lblPTypeR_display, 3, 1)
        grid.addWidget(self.lblPLocR_display, 3, 2)
        grid.addWidget(self.lblAIMVerR_display, 3, 3)

        self.setLayout(grid)
        self.setGeometry(300, 300, 800, 300)
        self.setWindowTitle("Stratacache PAC Manager")
        self.show()
   
    def onPStyleActivate(self, text):
        self.tstPStyleR = text
        print(self.tstPStyleR)
        self.lblPStyle.setText(text)
        self.lblPStyle.adjustSize()
        self.lblPStyleR_display.setText(text)
        self.lblPStyleR_display.adjustSize()
        return self.tstPStyleR

    def onPTypeActivate(self, text):
        self.tstPTypeR = text
        print(self.tstPTypeR)
        self.lblPTypeR.setText(text)
        self.lblPTypeR.adjustSize()
        self.lblPTypeR_display.setText(text)
        self.lblPTypeR_display.adjustSize()


    def onPLocActivate(self, text):
        self.strPLoc = text
        self.lblPLoc.setText(text)
        self.lblPLoc.adjustSize()
        self.lblPLocR_display.setText(text)
        self.lblPLocR_display.adjustSize()
        return self.strPLoc

    def onAIMVerActivate(self, text):
        self.strAIMVer = text
        self.label.setText(text)
        self.label.adjustSize()
        self.lblAIMVerR_display.setText(text)
        self.lblAIMVerR_display.adjustSize()
        return self.strAIMVer

def main():
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec())

if __name__ == '__main__':
    main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 09:06:30