a13131222 2020-09-17 12:39 采纳率: 0%
浏览 84
已采纳

练习pyqt5用户界面,代码是网上两个代码拼起来的,很奇怪,运行之后,不管是点击保存,还是run,都会闪退。但是把sys.stdout那行注释掉之后,可以运行,只是 hello那些字段无法定向到界面输出框里,求大佬教一下。

import sys
from PyQt5.QtCore import*
from PyQt5.QtWidgets import QWidget, QApplication, QGroupBox, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QLineEdit, QTextEdit
from PyQt5.QtGui import QIcon

class Stream(QThread):

newText = pyqtSignal(str)

def write(self, text):
    self.newText.emit(str(text))

class login(QWidget):
def init(self):
super().__init__()

    sys.stdout = Stream(newText=self.onUpdateText)
    self.initUi()
    self.initUI2()
    self.show()


#
def onUpdateText(self, text):
    # Write console output to text widget.
    cursor = self.process.textCursor()
    cursor.movePosition(QTextCursor.End)
    cursor.insertText(text)
    self.process.setTextCursor(cursor)
    self.process.ensureCursorVisible()

def closeEvent(self, event):
    """Shuts down application on close."""
    # Return stdout to defaults.
    sys.stdout = sys.__stdout__
    super().closeEvent(event)
#
def initUi(self):

    self.setWindowTitle("login")
    layout = QGridLayout()
    self.setGeometry(600, 600, 400, 400)

    btnGenMast = QPushButton('Run', self)
    btnGenMast.clicked.connect(self.genMastClicked)
    self.process = QTextEdit(self, readOnly=True)
    self.process.ensureCursorVisible()
    self.process.setLineWrapColumnOrWidth(500)
    self.process.setLineWrapMode(QTextEdit.FixedPixelWidth)




    nameLabel = QLabel("姓名")
    self.nameLineEdit = QLineEdit("  ")
    sexLabel = QLabel("性别")
    self.sexLineEdit = QLineEdit(" ")
    emitLabel = QLabel("手机号")
    self.phoneLineEdit = QLineEdit("")
    timeLabel = QLabel("邮箱")
    self.mailEdit = QLineEdit("")
    # layout.setSpacing(10)
    layout.addWidget(nameLabel,1,0)
    layout.addWidget(self.nameLineEdit,1,1)
    layout.addWidget(sexLabel, 2, 0)
    layout.addWidget(self.sexLineEdit, 2, 1)
    layout.addWidget(emitLabel,3,0)
    layout.addWidget(self.phoneLineEdit,3,1)
    layout.addWidget(timeLabel,4,0)
    layout.addWidget(self.mailEdit,4,1)
    layout.setColumnStretch(1, 10)
    save_Btn = QPushButton('保存')
    cancle_Btn = QPushButton('取消')

    cancle_Btn.clicked.connect(QCoreApplication.quit)
    save_Btn.clicked.connect(self.addNum)
    layout.addWidget(save_Btn)
    layout.addWidget(cancle_Btn)
    layout.addWidget(btnGenMast)
    layout.addWidget(self.process)
    self.setLayout(layout)

def initUI2(self):


    self.setWindowIcon(QIcon('1.png'))


def printhello(self):
    print('hello')

def genMastClicked(self):
    print('Running...')

    self.printhello()
    loop = QEventLoop()
    QTimer.singleShot(2000, loop.quit)
    loop.exec_()

    print('Done.')

def addNum(self): # 获取文本框内容

    name = self.nameLineEdit.text()
    sex = self.sexLineEdit.text()
    phone = self.phoneLineEdit.text()
    mail = self.mailEdit.text()

    print('姓名: %s 性别: %s 手机号: %s 邮箱: %s ' % (name,sex, phone, mail))

if name == '__main__':

app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
gui= login()

sys.exit(app.exec_())
  • 写回答

2条回答 默认 最新

  • 宁缺灬 2020-09-17 13:27
    关注

    恰巧3.4的py装这个比较麻烦。。盲答一下,关于python闪退,你如果走的bat打开,在bat后面加一个PAUSE,这样可以看具体运行报错内容。调试上肯定需要看到具体报错,才能决定下一步怎么做。。。我先装着,好了用你代码跑一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作