【问题标题】:Python stopped working error message while using this script?使用此脚本时 Python 停止工作错误消息?
【发布时间】:2018-02-24 12:35:35
【问题描述】:

它只在我运行此脚本时显示错误消息:

import tkinter as tk
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QImage, QPalette, QBrush
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QSize    

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(500, 500))    
        self.setWindowTitle("Chercher des Personnes")

        oImage = QImage("i.jpg")
        sImage = oImage.scaled(QSize(300,200))                   # resize Image to widgets size
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))                     # 10 = Windowrole
        self.setPalette(palette)



        self.nameLabel = QLabel(self)
        self.label = QLabel(self)
        self.Name = QLabel(self)
        self.Age = QLabel(self)
        self.Telnum = QLabel(self)
        self.Mail = QLabel(self)
        self.Adress = QLabel(self)

        self.nameLabel.setText('Prenom du personne:')
        self.Name.setText('Nom et Prenom: ')
        self.Age.setText('Age: ')
        self.Telnum.setText('Numero de Tel: ')
        self.Mail.setText('Email adress: ')
        self.Adress.setText('Adress: ')

        self.line = QLineEdit(self)

        self.line.move(150, 50)
        self.line.resize(200, 32)
        self.nameLabel.move(150, 20)
        self.Name.move(20, 150)
        self.Age.move(20, 180)
        self.Telnum.move(20, 210)
        self.Mail.move(20, 240)
        self.Adress.move(20, 270)

        pybutton = QPushButton('OK', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(200, 32)
        pybutton.move(150, 85)        

def clickMethod(self):
    class people:
        def __init__(self, full_name, birthday, telnum, mail, adress, other):
            self.name = full_name
            self.full_name = full_name
            self.birthday = birthday #ddmmyyyy
            self.telnum = telnum
            self.mail = mail
            self.adress = adress
            self.other = other


    #Peoples info
        john = people("Full name: John Doe", "Birthday: ??/01/2005 Age: 12", "Telephone number: +41 12 345 67 89", "Educanet2: fake@example", "Adress: 123 Fake Street, 1721 Suisse", "Other: John is a known programmer and plays ice hockey. He participates in robotics competitions. But he's bullied and made fun of at times")
         self.Name.setText(str(locals()[self.line.text().lower()].name ))








if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

这里的脚本上的缩进空格不正确,但它在我的真实代码上。为每行留出 4 个空格会花费我太长时间。

那么为什么我会收到这条消息:“Python 停止工作”? 我能做些什么来解决它?

Ps:使用其他脚本时不会出现这样的错误消息,或者当我用 tkinter 消息框替换 self.name.setText(" ") 时不会出现错误

【问题讨论】:

    标签: python python-3.x user-interface pyqt pyqt5


    【解决方案1】:

    我不完全确定您要在这里完成什么。但是,您的问题似乎来自您对 locals() 函数的使用。如果您只是想从行编辑中获取文本,请将该行更改为 self.Name.setText(self.line.text().lower())

    【讨论】:

    • 好吧,我正在尝试使用 locals() 来获取我存储在类中的信息。
    • 我想你可能对当地人的所作所为感到困惑。您编写代码的方式,仅当您在行编辑中输入的名称是“thibaut”时才有效,因为这是分配为人员类实例的变量。它永远不会有什么不同。所以在这种情况下,为什么不直接使用self.Name.setText(thibaut.name),因为它本质上是同一事物的一个简单得多的版本。此外,在运行 pyqt5 应用程序时,如果您从命令行/终端运行它,它会输出一条错误消息,以便您诊断正在发生的事情。
    • 那么为什么当我这样做时它会起作用:lines = [str(locals()[self.line.text().lower()].name), ' ', str(locals( )[self.line.text().lower()].birthday)] root = tkinter.Tk() root.withdraw() tkinter.messagebox.showinfo('Text', "\n".join(lines))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多