首页| 论坛| 消息
主题:解决第4章例子“30行的表达式求值”对PyQt5+Python3兼容性的问题。
回帖:解决了。继续更新。问题就出在text = unicode(self.lineedit.text())
这个地方。因为我安装的是python3.5,python3以上版本,默认都是unicode,所以把unicode去掉就OK了。pyqt5 + phython3的完整代码:
from __future__ import division
import sys
from math import *
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)
self.browser = QTextBrowser()
self.lineedit = QLineEdit("Type an expression and press Enter")
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(layout)
self.lineedit.setFocus()
#self.connect(self.lineedit, SIGNAL("returnPressed()"),
# self.updateUi)
self.lineedit.returnPressed.connect(self.updateUi)
self.setWindowTitle("Calculate")

def updateUi(self):
try:
text = self.lineedit.text()
self.browser.append("%s = %s" % (text, eval(text)))
except:
self.browser.append(
"%s is invalid!" % text)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
下一楼›:谢谢。

查看全部回帖(11)
«返回主帖