Reputation: 21
im fairly new to coding even more at object oriented python so please bear with me.
Im trying to build a GUI for a program i coded before using PyQt5.
I've designed my forms at Qt Designer then used python -m PyQt5.uic.pyuic -x [FILENAME].ui -o [FILENAME].py
to get the file as .py.
in order to avoid to change the file, im using another python file to call the form.py file and my program.py and build everything there
import sys
from myform import Ui_MainWindow
from mydialog1 import Ui_Dialog as Dlog_1
from mydialog2 import Ui_Dialog as Dlog_2
from PyQt5 import QtCore, QtGui, QtWidgets
from myprogram import *
class Prog(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(Prog, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
self.ui.actionAbout.triggered.connect(self.about)
self.ui.textBrowser.append(show())
def about(self):
dialog = QtWidgets.QDialog()
dialog.ui = Dlog_1()
dialog.ui.setupUi(dialog)
dialog.ui.Adicionar_3.clicked.connect(dialog.close)
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.exec_()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = Prog()
MainWindow.show()
sys.exit(app.exec_())
Im trying to display the output from a function (from myprogram) to a QTextBrowser widget as it can be seen self.ui.textBrowser.append(show())
although this works with plain text for example ("test") it wont work with a function.
the function show() is defined as the following
def show():
global empresas
info = ["Nome da empresa", "Site da empresa", "Montante investido"]
valid = False
while not valid:
try:
gen = ((k, v[0], v[1]) for k, v in empresas.items())
ename, esite, evalue = zip(*gen)
valid = True
except ValueError:
print('Não existem empresas para mostrar. Introduza mais empresas')
return
print("")
a = ('|{}{:^30}{}| |{}{:^30}{}| |{}{:^30}{}|'.format(c['g'],info[0],c['d'],c['g'],info[1],c['d'],c['g'],info[2],c['d']))
print("+" + "=" * (len(a)-32) + "+")
print(a)
print("+" + "=" * (len(a)-32) + "+")
for y in range(0,len(ename)):
if y % 2 == 0:
print(f'|{ename[y]:^30}| |{esite[y]:^30}| |{evalue[y]:^17.3f}Milhões de',simb, '|')
print("+" + "-" * (len(a)-32) + "+")
elif y % 2 == 1:
print(f'|{ename[y]:^30}| |{esite[y]:^30}| |{evalue[y]:^17.3f}Milhões de',simb, '|')
print("+" + "-" * (len(a)-32) + "+")
return
This function basicly prints an organized table with all the items from a dictionary to the console. Is there a way that allows me to print the same type of output to a QtextBrowser as it would look on the console?
Thanks in advance
Upvotes: 1
Views: 1857
Reputation: 2397
If you really do not want to alter your show
function to return a string instead of directly printing it, you could use stdout redirection for that. Basically this just tells print
to write into your own buffer, which you can afterwards flush into the actual target that the string should go to:
from contextlib import redirect_stdout
import io
# in __init__ :
f = io.StringIO() # in-memory string buffer
with redirect_stdout(f):
# everything printed in here will go to f
show()
# now reroute everything written to f to textBrowser
self.ui.textBrowser.append(f.getvalue())
Upvotes: 1