0

enter image description hereI'm trying to build a Python GUI app that adds information to a list with pyqt5 but I'm getting errors.the application should be able to display items that are entered into the lineedit inside the listwidget.

Below is the code:

from PyQt5 import QtWidgets, uic

app = QtWidgets.QApplication([])
form2 = uic.loadUi("login2.ui")

def add_item():
    if not form2.LineEdit_item.text() == "":
        form2.ListWidget.addItem(form2.LineEdit_item.text())
        form2.LineEdit_item.setText("")

form2.PushButton_addItem.clicked.connect(add_item)
form2.show()
app.exec()

and below is the error I'm getting:

Traceback (most recent call last):

  File "C:/Users/Windows 10/PycharmProjects/mygui2.py", line 11, in <module>
    form2.PushButton_addItem.clicked.connect(add_item)
AttributeError: 'QMainWindow' object has no attribute 'PushButton_addItem'

Process finished with exit code 1

How do i get this sorted out.

5
  • Is PushButton_addItem defined in your ui file ? Commented Sep 27, 2019 at 12:24
  • Push_Button is actually a button and addItem is a normal code with using List Widget which doesn't need to be defined and it wasn't defined on the video tutorials i was watching. Commented Sep 27, 2019 at 12:41
  • Please post your .ui file. It's easier to see what's wrong that way. Commented Sep 27, 2019 at 12:46
  • i just did that Commented Sep 27, 2019 at 12:52
  • okay thanks i really want to learn more including the database gui applications and more complex programs.i'll add you up now Commented Sep 27, 2019 at 14:34

1 Answer 1

2

Everything should work if you specify the name correctly for the objectName of theQPushButton widget.

enter image description here


enter image description here main.py

from PyQt5 import QtWidgets, uic

app = QtWidgets.QApplication([])

form2 = uic.loadUi("login2.ui")

def add_item():
    if not form2.LineEdit_item.text() == "":
        form2.ListWidget.addItem(form2.LineEdit_item.text())
        form2.LineEdit_item.setText("")

form2.PushButton_addItem.clicked.connect(add_item)
form2.show()
app.exec()

login2.ui

<?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>330</width>
    <height>258</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QListWidget" name="ListWidget"/>
    </item>
    <item>
     <widget class="QLineEdit" name="LineEdit_item"/>
    </item>
    <item>
     <widget class="QPushButton" name="PushButton_addItem">
      <property name="text">
       <string>PushButton</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>330</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.