PYQT5连接按钮按下堆叠widget

发布于 2025-02-12 10:56:17 字数 472 浏览 0 评论 0原文

我只是将.UI文件转换为PY文件,并尝试使用信号和插槽系统将QPUSHBUTTON链接到QSTACKEDWIDGET。这就是我尝试做的事情:

self.createButton.clicked.connect(self.stackedWidget.setCurrentIndex(2)) 

应该将堆叠的小部件的索引设置为2个按钮。但是,在运行时发生以下错误:

TypeError: argument 1 has unexpected type 'NoneType'

不确定是什么参数1,但是占位符的代码:

self.createButton.clicked.connect(self.stackedWidget.update)

似乎有效,除了我想实现的目标不是。我的代码怎么了?

I just converted my .ui file into the py file and tried to link a QPushButton to a QStackedWidget using the signals and slots system. This is what I attempted to do:

self.createButton.clicked.connect(self.stackedWidget.setCurrentIndex(2)) 

and it was supposed to set the index of the stacked widget to 2 on button click. However at runtime the following error occurred:

TypeError: argument 1 has unexpected type 'NoneType'

Was not really sure what argument 1 was, but the placeholder code of:

self.createButton.clicked.connect(self.stackedWidget.update)

seemed to work except that was not what I want to achieve. What is wrong with my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2025-02-19 10:56:17

当启用点击事件时,连接函数将作为参数作为一个函数要调用,因此您应该将statment包裹在lambda函数中,如下所示:

self.createButton.clicked.connect(lambda: self.stackedWidget.setCurrentIndex(2)) 

the the connect function takes as a parameter a function to call when the click event is emited so you should wrap your statment in a lambda function as follows :

self.createButton.clicked.connect(lambda: self.stackedWidget.setCurrentIndex(2)) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文