我的应用由QMainWindow
与QToolBar
组成。我的目的是点击一个QToolBar
元素并在一个单独的窗口(QDialog
)中打开一个日历。PyQt5 - 显示不同类别的QDialog
我想创建一个单独的类QDialog
并称它从QMainWindow
显示。
这是我QDialog
,只是一个日历:
class CalendarDialog(QDialog):
def __init__(self):
super().__init__(self)
cal = QCalendarWidget(self)
现在从QMainWindow
我想显示的动作触发条件后,旁边的日历:
class Example(QMainWindow):
...
calendarAction.triggered.connect(self.openCalendar)
...
def openCalendar(self):
self.calendarWidget = CalendarDialog(self)
self.calendarWidget.show()
它不工作。在调用openCalendar
的事件之后,应用程序关闭而不打印任何输出错误。我已经把一些打印调试,并且CalendarDialog.__init__(self)
甚至没有被调用。
关于向QToolBar
的代码如下:
openCalendarAction = QAction(QIcon(IMG_CALENDAR), "", self)
openCalendarAction.triggered.connect(self.openCalendar)
self.toolbar.addAction(openCalendarAction)
你不是在这行'self.calendarWidget = SMCalendarWidget(self)'创建'CalendarDialog','SMCalendarWidget'是否存在? – Ceppo93
是的,你是对的。这是一个转录错误。我已更正了代码。 – user2607702
好的,你可以在“创建”toolBar时分享代码吗?提供的似乎几乎是正确的,除了'CalendarDialog .__ init __(self)'不带任何参数(self是隐含的),并且你用一个参数调用它'CalendarDialog(self)',可能你想指定一个'parent'参数'__init__'。 – Ceppo93