roudan
roudan

Reputation: 4252

PyQt5 need help on line edit

I am learning PyQt5 and did a simple code below. I got a few questions:

  1. After run the code and input text in line edit then click push button, I got an error of AttributeError: 'MyForm' object has no attribute 'leName'. How to fix it?, I think leName is not correct name?
  2. Then I remove self.leName.text(), I clicked on the button, no message is displayed. Not sure how it works?
  3. After closed the Qt window, I tried to re-run it in Spyder, it can not run it anymore. so I have to exit Spyder and relaunch spyder in order to run it again. Why?
import sys
from PyQt5.QtWidgets import *
#from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.uic import loadUi

class MyForm(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        loadUi("demoLE.ui",self)
        
        self.setWindowTitle("Demonstrates how to use Line Edit Widget")
        self.pbClick.clicked.connect(self.display_message)
        
    def display_message(self):
        self.label_2.setText("Hello, "+self.leName.text())
        
        
if __name__=="__main__":
    app=QApplication(sys.argv)
    ex=MyForm()
    ex.show()
    sys.exit(app.exec_())

the contents of demoLE.ui is:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>330</x>
      <y>150</y>
      <width>171</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>Enter Your Name</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>320</x>
      <y>190</y>
      <width>68</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>460</x>
      <y>150</y>
      <width>113</width>
      <height>25</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pbClick">
    <property name="geometry">
     <rect>
      <x>320</x>
      <y>220</y>
      <width>112</width>
      <height>34</height>
     </rect>
    </property>
    <property name="text">
     <string>Click</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

enter image description here

Upvotes: 0

Views: 199

Answers (1)

TheEagle
TheEagle

Reputation: 5982

The name of the QLineEdit is lineEdit, not leName, so change self.label_2.setText("Hello, " + self.leName.text()) to self.label_2.setText("Hello, " + self.lineEdit.text())

Upvotes: 1

Related Questions