Python Forum
Interfacing from Python to GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interfacing from Python to GUI
#1
Hi.

I have written some Python code and is works OK. I now need to, say, interface it better with the user, myself Wink

I got QT Designer and configured some dialogues including a textEdit widget called textEdit_log.

The code which I don’t understand much, looks like this:

from PyQt5.QtWidgets import *
from PyQt5 import uic

def main():
    app = QApplication([])
    window = PlotGUI()
    app.exec_()


if __name__ == '__main__':
    main()


class PlotGUI(QMainWindow):

    def __init__(self):
        super(PlotGUI, self).__init__()
        uic.loadUi("PlotGUI.ui", self)
        self.show()

        self.textEdit_log.append("Hi!")
As a result of last line above “Hi!” shows up in textEdit_log. Perfect

The problem is that I am struggling to find out how can I append text to textEdit_log from outside PlotGUI code, I mean, from ‘regular’ Python code.

Having understood how to solve this problem I will probably solve the other similar difficulties.

I hope this post is according to the rules Smile

Thank you
H. Martins
Reply
#2
Don't use from xxx import * !

You can append text for example by using argv

python3 test.py "Hello World"

test.py

from PyQt5 import QtWidgets
import sys 
 
class PlotGUI(QtWidgets.QMainWindow):
 
    def __init__(self):
        super(PlotGUI, self).__init__()
        self.textEdit_log = QtWidgets.QTextEdit()
        self.setCentralWidget(self.textEdit_log)
        self.show()
        self.textEdit_log.append("Hi!")

 
 
if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = PlotGUI()
    if len(sys.argv) > 1:
        window.textEdit_log.append(sys.argv[1])
    sys.exit(app.exec_())
Reply
#3
Hi,

Looks like that solution involves application (itself) callings arguments:

On the other side, I am having the following

self.pushButton_Start.clicked.connect(main_proc)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'PlotGUI' object has no attribute 'pushButton_Start'


Looks like it does not recognise the other widgets. Could this be caused by having removed:

from PyQt5.QtWidgets import *

Meanwhile I need to send some data from say Python non-windows code to PlotGUI window's textEdit_log, not command line data.

Thanks
H. Martins
Reply
#4
It's hard to answer without knowing the code. How do you run that non-windows code?
Reply
#5
(Apr-20-2023, 03:56 PM)Axel_Erfurt Wrote: It's hard to answer without knowing the code. How do you run that non-windows code?

I mean, nothing related with a GUI, just reading and processing data files, writing files, outputing data to a COM port and printing related staff in PyCharm monitor(?) output.

Of course I can do the job if I scratch the code as circumstances change. I could also use the command line to configure software behaviour but it wold be much easier if these parameter would be configured in a GUI and some intermediate processing massages output to a textEdit widget.

I can't find out the proper syntax to send text from say non GUI related code to the GUI code. Looks like something is missing that I am not aware and can't find out.

Suppose I need to send text from one of my previously written (non GUI) functions to a textEdit widget. I can't find out the proper syntax.

Thanks
H. Martins
Reply
#6
In

class PlotGUI(QMainWindow):

    def __init__(self):
        super(PlotGUI, self).__init__()
        uic.loadUi("PlotGUI.ui", self)
        self.show()

        self.pushButton_Start.clicked.connect(main_proc)

        self.textEdit_log.append("Hi!")
The line:
self.pushButton_Start.clicked.connect(main_proc)

main_proc() has no relation to the graphical environment. The above line makes it all run. What about having messages in the PlotGUI coming from the main_proc()?

H. Martins
Reply
#7
You mean something like this? pushButton_Start receives text from the other script.

from PyQt5 import QtWidgets
import sys 
import no_gui
  
class PlotGUI(QtWidgets.QMainWindow):
    def __init__(self):
        super(PlotGUI, self).__init__()
        
        self.setGeometry(50, 50, 400, 300)
        self.textEdit_log = QtWidgets.QTextEdit()
        self.setCentralWidget(self.textEdit_log)
        self.pushButton_Start = QtWidgets.QPushButton("Start")
        self.pushButton_Start.clicked.connect(self.button_clicked)
        self.statusBar().addPermanentWidget(self.pushButton_Start)
        self.textEdit_log.append("Hi!")
        
    def button_clicked(self):
        returned_text = no_gui.send_text()
        self.textEdit_log.append(f"text received from no_gui: {returned_text}")
        self.statusBar().showMessage("reading text from no_gui ...")
 
 
if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = PlotGUI()
    window.show()
    sys.exit(app.exec_())
no_gui.py

def send_text():
    my_string = "Hello World"
    return my_string
Reply
#8
I found a solution:

class PlotGUI(QMainWindow):

    def __init__(self):
        super(PlotGUI, self).__init__()
        uic.loadUi("PlotGUI.ui", self)
        self.show()

        global txt_log                     #  <<<<<<<<<<<<<< these two lines
        txt_log = self.textEdit_log  #  <<<<<<<<<<<<<< these two lines
Then a small aggregating (append + repaint) function

def txt2log(str):
    txt_log.append(str)
    txt_log.repaint()
And finnaly,

txt2log("appending text")
placed in any other function is doing the job.

Thank you
H. Martins
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,501 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Interfacing with a USB game controller barryjo 2 8,903 Oct-31-2016, 08:21 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

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