首页
学习
活动
专区
圈层
工具
发布

PyQt5编码
EN

Stack Overflow用户
提问于 2019-09-29 10:03:10
回答 1查看 26关注 0票数 0
代码语言:javascript
复制
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_())

我在这个代码中有两个问题:

  1. ,如果我在行中输入字母,编辑输出就来了。但是,当我清除行,编辑并再次输入之前的输出仍然在那里和输出放在一起.我想要的方法是,当我清除行时,编辑我需要的输出也要清除。

  1. ,如果我输入出的字母,爱情应用程序就会关闭。我不想那样。

这段代码使TkInter很好地工作,但是Pyqt5显示了这个问题。

EN

回答 1

Stack Overflow用户

发布于 2019-09-29 10:34:22

您只需要在请求每个操作时重置self.out值:

代码语言:javascript
复制
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动态清除或设置输出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58154187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档