pyqt calendar I want to pass date value to another class

时间:2019-04-17 02:47:00

标签: python pyqt pyqt5

When the calendar is clicked, it tries to pass a value to def Calendar_click (self, date): value in class Tab1 (QWidget): def ViewTable (self) :. I tried to specify parameters, I also specified global variables, but I did not receive the data. def MotherInformation (self): I continue to refer to tabs.addTab (Tab1 (), 'TAB1') in the function. How do I fix it?

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) 

1 个答案:

答案 0 :(得分:1)

您必须保存创建的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)