pyqt5中的lambda函数 - 返回None而不是预期的值

时间:2017-11-19 19:15:48

标签: python-3.x lambda pyqt pyqt4 pyqt5

我正在Mark Summerfield的书上学习PyQt,但是使用python 3和PyQt5我需要将所有代码转换为新语法,我在这个例子中遇到了一些问题:

IFS=,
while read a b c d dat remaining
do
    dat=${dat//\// }  # remove all / from date-field
    dat=${dat/:/ }    # remove first : from date-field
    echo "$a,$b,$c,$d,$(date --date="$dat" "+%Y-%m-%d %H:%M:%S"),$remaining"
done < redate.dat

当我点击按钮1和2时,程序按预期运行,但是当我点击按钮3时,我收到以下消息: “你点击按钮'False'”,而我期待“你点击按钮'三'”。

所以,我认为我对lambda函数的语法做错了,但我看不出是什么。

这里是pyqt4和python 2的原始代码:

import sys
from functools import partial
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form,self).__init__(parent)

        button1 = QPushButton('One')
        button2 = QPushButton('Two')
        button3 = QPushButton('Three')
        button4 = QPushButton('Four')               
        button5 = QPushButton('Five')
        self.label = QLabel('Click a button...')

        layout = QHBoxLayout()
        layout.addWidget(button1)
        layout.addWidget(button2)
        layout.addWidget(button3)
        layout.addWidget(button4)
        layout.addWidget(button5)
        layout.addStretch()
        layout.addWidget(self.label)
        self.setLayout(layout)

        button1.clicked.connect(self.one)

        #partial for passing runtime argument to function, useful for using one slot for many signals
        button2.clicked.connect(partial(self.anyButton,'Two'))

        button3.clicked.connect(lambda who='Three': self.anyButton(who))


    def one(self):
        self.label.setText("You clicked button 'One'")

    def anyButton(self,who):
        self.label.setText("You clicked button '%s'" %who)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

0 个答案:

没有答案