单击日历时,它会尝试将值传递给 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/