Python Forum
[PyQt] Collect entry from textline Widget via UI file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Collect entry from textline Widget via UI file
#1
Hi,

I would like to know how I can access a QLineEdit widget in a UI file directly from Python.
I have read some examples on the Internet but, I can't get it to work.

This is what I have:
original *.ui file: propertyDialog.ui
extracted *.py file: propertyDialog.py
QLineEdit widget name on the ui: "input_PipeName"

The script I have so far is:
from PyQt5 import QtCore, QtGui, QtWidgets
from propertyDialog import Ui_Dialog

class mainProgram(QtWidgets.QMainWindow, Ui_Dialog):
    def __init__(self, parent=None):
        self.cntPipe = 0
        self.cntLiner = 0
        self.cntCoating = 0
        self.pipeproperty = {}
        self.coating = {}
        self.liner = {}
        super(mainProgram, self).__init__(parent)
        # self.calc = Calculations()
        self.setupUi(self)
        self.response_AddPipe.clicked.connect(self.add_pipe)

    def add_pipe(self):
        self.cntPipe +=1
        self.cntLiner = 0
        self.cntCoating = 0
        self.pipeproperty[self.cntPipe]={}
        pipe_name = self.inputPipe_name.text()
        self.pipeproperty[self.cntPipe]["Name"] = pipe_name

if __name__ == "__main__":

    import sys
    app = QtWidgets.QApplication(sys.argv)
    nextGui = mainProgram()
    nextGui.show()
    sys.exit(app.exec_())
With the error:
Error:
"C:\Users\Usr\Desktop\Work Folder PPM\VirtualEnvironment\Scripts\python.exe" "C:/Users/Usr/Desktop/Work Folder PPM/VirtualEnvironment/Scripts/Total.py" Traceback (most recent call last): File "C:/Users/Usr/Desktop/Work Folder PPM/VirtualEnvironment/Scripts/Total.py", line 151, in <module> nextGui = mainProgram() File "C:/Users/Usr/Desktop/Work Folder PPM/VirtualEnvironment/Scripts/Total.py", line 15, in __init__ self.response_AddPipe.clicked.connect(self.add_pipe()) File "C:/Users/Usr/Desktop/Work Folder PPM/VirtualEnvironment/Scripts/Total.py", line 23, in add_pipe pipe_name = self.inputPipe_name.text() AttributeError: 'mainProgram' object has no attribute 'inputPipe_name' Process finished with exit code 1
Can someone help me with this? It seems to fail on:
pipe_name = self.inputPipe_name.text()
Reply
#2
If you plan to use Ui file, you don't need to compile it to python. Doing the conversion in the script will make your workflow much easier. You can use Qt's uic utility like this:

#!/usr/bin/python3
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
 
LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))
 
 
class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "/foo.ui", self)
        self.show()
        print(self.ui.inputPipe_name.text())
 
if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
The only downside is that it takes more time to execute, but if you plan the distribute the package, the ui files can be converted only once at install time.

If you really want to convert it, you must first import it in your script, then in __init__:

        self.ui = foo.Ui_MainWindow()  # Or .Ui_Dialog, or .Ui_Widget
        self.ui.setupUi(self)
Then access yours widgets trought the self.ui object, as in the previous example
Reply
#3
Thanks, it is my first try with a GUI but, how would you collect the data from the textline using a button?
The button is called 'AddPipe' and should call an function that collects the data from several textlines and stores it in a dictionary. I am struggling with how I should write the function. I thought it would be as below but it terminates the script without error.

import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))

self.ui = foo.Ui_MainWindow()  # Or .Ui_Dialog, or .Ui_Widget
self.ui.setupUi(self)

class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "/propertyDialog.ui", self)
        self.show()
        self.response_AddPipe.clicked.connect(self.call_property)


    def call_property(self):
        print(self.ui.inputPipe_name.text())


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
Reply
#4
Note as I have advised numerous folks already -- it will be best for you not to use the Designer nor UI files when learning PyQt as the Designer and UI files are not how PyQt was actually designed to work and the garbage code that the Designer barfs out is extremely difficult to work with even if you are an experienced PyQt professional. The alternative (once you understand it which actually does not take that long to do even for the inexperienced) is just as fast or faster than using the Designer and has the added benefit of being at least 5 times easier to work with and maybe even 10 times easier because you will have a better understanding of how all the pieces fit together and you can do a lot of copy/paste with minor tweaks from previous projects.

If you need help with this I have a Discord channel where I am tutoring (at no charge but donations always accepted) a few pyqt novices if you are interested I will help you learn to fish -- aka that means you have to do the work because I will not give you the fish just the fishing pole (basic framework), the bait (suggestions), and advice on what bait to use and where best to fish (things to google and/or url links to help you get the information you need -- the actual catching of the fishing (producing finished code) is all up to you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 490 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] entry widget DPaul 5 1,426 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  [PyQt] [solved] How to display a pdf-file in a PyQt6 widget BigMan 13 15,660 May-06-2023, 09:27 AM
Last Post: Axel_Erfurt
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,871 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] compare entry with value inside file rwahdan 1 2,051 Jun-19-2021, 08:01 AM
Last Post: Yoriz
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,212 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 6,647 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  Android app to collect data jehoshua 7 4,279 Dec-24-2020, 08:07 AM
Last Post: dejaolsone
  [Tkinter] password with Entry widget TAREKYANGUI 9 5,772 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,294 Jul-13-2020, 10:34 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020