from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QRect
import sys
class Mywindow(QMainWindow):
def __init__(self):
super(Mywindow, self).__init__()
self.out = ""
self.con = {'L': '1', 'O': '2', 'V': '3', 'E': '4'}
self.setFixedSize(400, 400)
self.btn1 = QtWidgets.QPushButton(self)
self.lab1 = QtWidgets.QLabel(self)
self.lab2 = QtWidgets.QLabel(self)
self.line1 = QtWidgets.QLineEdit(self)
self.initUI()
self.show()
def initUI(self):
self.btn1.setText("Enter")
self.btn1.move(200, 150)
self.btn1.clicked.connect(self.action)
self.lab1.setText("input :")
self.lab1.setStyleSheet('font-size:20px ;color:blue')
self.lab1.move(100, 150)
self.line1.text()
self.line1.move(80, 200)
def action(self):
for i in self.line1.text():
self.out += self.con[i.upper()]
self.lab2.setText(self.out)
self.lab2.setStyleSheet('font-size:30px ; color :red')
self.lab2.move(200, 250)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Mywindow()
win.show()
sys.exit(app.exec_())我在这个代码中有两个问题:
,
这段代码使TkInter很好地工作,但是Pyqt5显示了这个问题。
发布于 2019-09-29 10:34:22
您只需要在请求每个操作时重置self.out值:
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QRect
import sys
class Mywindow(QMainWindow):
def __init__(self):
super(Mywindow, self).__init__()
self.out = ""
self.con = {'L': '1', 'O': '2', 'V': '3', 'E': '4'}
self.setFixedSize(400, 400)
self.btn1 = QtWidgets.QPushButton(self)
self.lab1 = QtWidgets.QLabel(self)
self.lab2 = QtWidgets.QLabel(self)
self.line1 = QtWidgets.QLineEdit(self)
self.initUI()
def initUI(self):
self.btn1.setText("Enter")
self.btn1.move(200, 150)
self.btn1.clicked.connect(self.action)
self.lab1.setText("input :")
self.lab1.setStyleSheet('font-size:20px ;color:blue')
self.lab1.move(100, 150)
self.line1.text()
self.line1.move(80, 200)
def action(self):
#clear output
self.out = ''
for i in self.line1.text():
self.out += self.con[i.upper()]
self.lab2.setText(self.out)
self.lab2.setStyleSheet('font-size:30px ; color :red')
self.lab2.move(200, 250)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Mywindow()
win.show()
sys.exit(app.exec_())还可以使用keyPressEvent动态清除或设置输出。
https://stackoverflow.com/questions/58154187
复制相似问题