gpt4 book ai didi

python - PyQt5在两个类之间传递参数: lambda vs partial

转载 作者:行者123 更新时间:2023-12-01 01:06:52 24 4
gpt4 key购买 nike

我正在尝试在两个 PyQt5 类之间传递参数。我使用了三种方法:

  1. 使用 lambda 函数。
  2. 包装函数(类似于 lambda 函数)。
  3. partial来自functools模块。

在下面的示例中,我有两个窗口:

  • 主窗口有 QLineEdit mw_line_edit和一个 QPushButton mw_open_new_dialog_button .
  • 对话框:有一个 QLineEdit line_edit和 aQPushButton push_button

当我点击按钮push_button时,我想让它插入line_edit的内容至mw_line_edit .

这是一个最小的例子:

import sys
from functools import partial

from PyQt5 import QtWidgets, QtGui, QtCore


class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.central_widget)
self.mw_open_new_dialog_button = QtWidgets.QPushButton('Open New dialog', self)
self.mw_line_edit = QtWidgets.QLineEdit(self)
self.hlayout = QtWidgets.QHBoxLayout(self)
self.hlayout.addWidget(self.mw_open_new_dialog_button)
self.hlayout.addWidget(self.mw_line_edit)
self.central_widget.setLayout(self.hlayout)

self.mw_open_new_dialog_button.clicked.connect(self.open_new_dialog)


def open_new_dialog(self):
self.dlg = Dialog()
#self.dlg.clicked.connect(partial(self.write_something, self.dlg.line_edit.text())) # <<<<<<< This does not work
self.dlg.clicked.connect(lambda: self.write_something(self.dlg.line_edit.text())) # this works
#self.dlg.clicked.connect(self.wrapper(self.dlg.line_edit.text()))# <<<<<<<<<<This does not work
self.dlg.exec()

@QtCore.pyqtSlot()
def write_something(self, text):
self.mw_line_edit.setText(text)

def wrapper(self, text):
return lambda: self.write_something(text)



class Dialog(QtWidgets.QDialog):

clicked = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(QtWidgets.QDialog, self).__init__(parent)
self.hlayout = QtWidgets.QHBoxLayout(self)
self.line_edit = QtWidgets.QLineEdit(self)
self.push_button = QtWidgets.QPushButton('Click me', self)
self.hlayout.addWidget(self.line_edit)
self.hlayout.addWidget(self.push_button)
self.label = QtWidgets.QLabel('I am a Qlabel', self)
self.hlayout.addWidget(self.label)
self.setLayout(self.hlayout)
self.push_button.clicked.connect(self.clicked)

def write_something(self, text):
print(text)



app = QtWidgets.QApplication(sys.argv)

main_window = MainWindow()
main_window.show()
sys.exit(app.exec())

正如您在注释行中看到的,只有以下方法有效:

self.dlg.clicked.connect(lambda: self.write_something(self.dlg.line_edit.text()))

为什么其他两个不起作用,即:

self.dlg.clicked.connect(partial(self.write_something, self.dlg.line_edit.text())) # <<<<<<< This does not work

self.dlg.clicked.connect(self.wrapper(self.dlg.line_edit.text()))# <<<<<<<<<<This does not work

谢谢

最佳答案

1) functools.partial()

您要传递给partial的参数是什么?您当时正在传递方法write_somethingself.dlg.line_edit的文本连接已建立。

该文本的值是多少?它是一个空字符串,这解释了失败。

这种情况有什么解决方案吗?是的,不传递文本,而是传递QLineEdit,并在方法write_something中获取文本并将其设置在另一个QLineEdit中:

def open_new_dialog(self):
self.dlg = Dialog()
self.dlg.clicked.connect(partial(self.write_something, self.dlg.line_edit))
self.dlg.exec()

def write_something(self, le):
self.mw_line_edit.setText(le.text())

2) 包装器

这是同样的问题,您正在传递连接时的空文本

有什么解决办法吗?有,和上一个一样的解决办法。

def open_new_dialog(self):
self.dlg = Dialog()
self.dlg.clicked.connect(self.wrapper(self.dlg.line_edit))
self.dlg.exec()

def write_something(self, text):
self.mw_line_edit.setText(text)

def wrapper(self, line):
return lambda: self.write_something(line.text())
<小时/>

会有一个干净的解决方案吗?是的,创建一个在您点击时传输文本的信号。

from PyQt5 import QtWidgets, QtGui, QtCore

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
self.mw_open_new_dialog_button = QtWidgets.QPushButton('Open New dialog')
self.mw_line_edit = QtWidgets.QLineEdit()
hlayout = QtWidgets.QHBoxLayout(central_widget)
hlayout.addWidget(self.mw_open_new_dialog_button)
hlayout.addWidget(self.mw_line_edit)
self.mw_open_new_dialog_button.clicked.connect(self.open_new_dialog)

@QtCore.pyqtSlot()
def open_new_dialog(self):
self.dlg = Dialog()
self.dlg.textSignal.connect(self.mw_line_edit.setText)
self.dlg.exec()

class Dialog(QtWidgets.QDialog):
textSignal = QtCore.pyqtSignal(str)

def __init__(self, parent=None):
super(QtWidgets.QDialog, self).__init__(parent)
hlayout = QtWidgets.QHBoxLayout(self)
self.line_edit = QtWidgets.QLineEdit()
self.push_button = QtWidgets.QPushButton('Click me')
hlayout.addWidget(self.line_edit)
hlayout.addWidget(self.push_button)
self.label = QtWidgets.QLabel('I am a Qlabel')
hlayout.addWidget(self.label)
self.push_button.clicked.connect(self.sendText)

@QtCore.pyqtSlot()
def sendText(self):
self.textSignal.emit(self.line_edit.text())

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())

关于python - PyQt5在两个类之间传递参数: lambda vs partial,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55255980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com