微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何防止在 TableWidget 的第一行上为每个创建的 ComboBox 编写额外的代码行?

如何解决如何防止在 TableWidget 的第一行上为每个创建的 ComboBox 编写额外的代码行?

我想在 TableWidget 的第一行创建一个 ComboBox。我的代码到目前为止有效,但问题是我必须为每个单元格的每个 ComboBox 编写另一行。我使用这些 ComboBoxes 对每一列进行分类,以从这些列中检索数据以进行计算。有没有办法缩短代码?非常感谢。

我的代码

from PyQt5 import QtCore,QtGui,QtWidgets

class ComboBoxCategory(QtWidgets.QComboBox):
    def __init__(self,parent):
        super().__init__(parent)
        self.setStyleSheet("font-size: 13px")
        self.addItems(["","Customer-ID","Customer Name","Revenue","Year","Revenue Type"])

class Ui_MainWindow(object):

    def setupUi(self,MainWindow):
        MainWindow.resize(1700,800)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setobjectName("centralwidget")
        #****************   Table Widget  **********************#
        self.TableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.TableWidget.setRowCount(500)
        self.TableWidget.setColumnCount(26)
        self.TableWidget.setHorizontalHeaderLabels(list("ABCDEFGHIJKLMnopQRSTUVWXYZ"))
        self.TableWidget.setColumnWidth(0,200)
        self.TableWidget.horizontalHeader().resizeSections()
        self.TableWidget.setGeometry(QtCore.QRect(40,200,1625,500))
        # # # ************   Table ComboBox first row  ****************#
        self.combo0 = ComboBoxCategory(self.TableWidget)
        self.TableWidget.setCellWidget(0,self.combo0)
        self.combo1 = ComboBoxCategory(self.TableWidget)
        self.TableWidget.setCellWidget(0,1,self.combo1)
        self.combo2 = ComboBoxCategory(self.TableWidget)
        self.TableWidget.setCellWidget(0,2,self.combo2)

编辑:已解决

  def ComboBox(self):
        for column in range(2):
            combo_cell = "combo" + "0"
            self.combo_cell = ComboBoxCategory(self.TableWidget)
            self.TableWidget.setCellWidget(0,column,self.combo_cell)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。