python - pyqt 日历 我想将日期值传递给另一个类

标签 python pyqt pyqt5

单击日历时,它会尝试将值传递给 def Calendar_click (self, date):class Tab1 (QWidget): def ViewTable (self) : 。我尝试指定参数,也指定全局变量,但没有收到数据。 def MotherInformation (self):我继续引用tabs.addTab (Tab1 (), 'TAB1')在函数中。我该如何修复它?

TypeError: ViewTable() missing 1 required positional argument: 'caldate'
class main_window(QWidget):
    def __init__(self):
        super(main_window, self).__init__()
        ...

    def Calendar(self):
        self.cal = QCalendarWidget(self)
        self.cal.setGridVisible(True)
        vbox = QVBoxLayout()
        vbox.addWidget(self.cal)
        self.calGroup = QGroupBox(title='day')
        self.calGroup.setLayout(vbox)
        self.cal.clicked.connect(self.Calendar_click)

    def Calendar_click(self, date):
        global calendarDate
        calendarDate = date
        # Tab1().ViewTable(date)
        # calendarDate = QDate.toPyDate(date)

    def MotherInformation(self):    
        vbox.addWidget(tabs)
        tabs.addTab(Tab1(), 'TAB1') # this value -> Class Tab1 ??
        tabs.addTab(QPushButton(), 'TAB2')
        self.lineGroup1.setLayout(vbox)

class Tab1(QWidget):
    def __init__(self):
        super(Tab1, self).__init__()
        self.ViewTable()

    def ViewTable(self, caldate):
        print(caldate)
        tab1TableWidget = QTableWidget()
        tab1TableWidget.resize(613,635)
        tab1TableWidget.horizontalHeader()
        tab1TableWidget.setRowCount(100)
        tab1TableWidget.setColumnCount(100) 

最佳答案

您必须保存创建的 Tab1 对象的引用并在需要时使用它:

def Calendar_click(self, date):
    self.tab1.ViewTable(date)

def MotherInformation(self):
    self.tab1 = Tab1()  # save reference
    vbox.addWidget(tabs)
    tabs.addTab(self.tab1, 'TAB1')
    tabs.addTab(QPushButton(), 'TAB2')
    self.lineGroup1.setLayout(vbox)

您遇到的另一个错误是您在 Tab1 构造函数中不必要地调用 ViewTable() 方法,更改为:

class Tab1(QWidget):
    def ViewTable(self, caldate):
        print(caldate)
        tab1TableWidget = QTableWidget()
        tab1TableWidget.resize(613,635)
        tab1TableWidget.horizontalHeader()
        tab1TableWidget.setRowCount(100)
        tab1TableWidget.setColumnCount(100) 

关于python - pyqt 日历 我想将日期值传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55719164/

相关文章:

python - 张量维度的大小范围 - tf.range

python - 我将如何为每个小部件元素设置单独的颜色?

python - 如何在使用 QSplitter 时固定一个小部件

python - PyQt5 为每个组合框选项呈现不同的复选框集

Python如何从QDateTimeEdit中提取字符串

python - 我想将文本放入 pyqt QCalendarWidget

python - matplotlib Axes3D 中的 mayavi 3d 对象

python - SQLAlchemy 基于字典对约束进行建模

python - TAB 键在我的 PYQT5 和 Python 代码中不起作用

python - 在selenium.py中,如何处理404状态码