Home > database >  Cannot get value of an input field
Cannot get value of an input field

Time:08-31

I cannot seem to get the value of the input field.

The error I get is 'bool' object has no attribute 'input'. Yet I am fairly certain I have set the pointer up properly.

I have tried converting the Ui file to Py and still get the same result. I feel like I am calling the wrong object here, but every tutorial online shows using .text() to get the value of a QLineEdit child.

Python File:

from PyQt5 import QtWidgets, uic
import sys


class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__() # Call the inherited classes __init__ method
        uic.loadUi('main.ui', self) # Load the .ui file
        self.show() # Show the GUI

        self.button = self.findChild(QtWidgets.QPushButton, 'Button')
        self.button.clicked.connect(Ui.button_pressed)

        self.input = self.findChild(QtWidgets.QLineEdit, 'userInput')

    def button_pressed(self):
        print("Hello"   self.input.text())


    
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

Ui File

    <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget  name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>417</width>
    <height>101</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget  name="centralwidget">
   <widget  name="Button">
    <property name="geometry">
     <rect>
      <x>330</x>
      <y>30</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Submit</string>
    </property>
   </widget>
   <widget  name="userInput">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>30</y>
      <width>291</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget  name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>417</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget  name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

CodePudding user response:

Using self.button.clicked.connect(self.button_pressed) works. The argument passed as "self" to Ui.button_pressed appears to just be a bool indicating if it was clicked (I'm honestly not sure), rather then 'self' (the actual class instantiation).

CodePudding user response:

The issue is due to the keyword self being set to a boolean. Hence, self.input generates the error you encountered. As Alec points out, the self is not being set correctly as you are passing Ui.button_pressed instead of self.button_pressed as the argument to self.button.clicked.connect().

  • Related