Hendrra
Hendrra

Reputation: 859

Printing multiple lines in a window using PyQT5 and Python

I am trying to code a very simple window application. I want it to print some "output" in a part of a window (or in a new one - it doesn't matter).

I will try to better explain my problem using the code below.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import pyqtSlot, QSize, QRect

class PrintWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title='Print many lines'
        self.left=10
        self.top=10
        self.width=640
        self.height=480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)

        self.statusBar().showMessage('In progress')

        label1 = QLabel("Choose how many lines would you like to print:", self)
        label1.setGeometry(QtCore.QRect(label1.x(), label1.y(), label1.width()+150, label1.height()))
        label1.move(10, 20)    

        centralWidget = QWidget(self)          
        self.setCentralWidget(centralWidget)  

        self.comboBox = QComboBox(centralWidget)
        self.comboBox.setGeometry(QRect(40, 40, 491, 31))
        self.comboBox.setObjectName(("comboBox"))
        self.comboBox.addItem("")
        self.comboBox.addItem("1")
        self.comboBox.addItem("10")
        self.comboBox.addItem("50")
        self.comboBox.addItem("100")
        self.comboBox.move(10, 60)

        button_search = QPushButton('PRINT', self)
        button_search.clicked.connect(self.PrintFunction)
        button_search.resize(200,50)
        button_search.move(220,300)  

        self.show()

    def PrintFunction(self):
        x = self.comboBox.currentText()
        if len(x) == 0:
            x = 0
        else:
            x = int(x)

        for i in range(1, x+1):
            print('That is line number: ', i, ' , ', x - i, ' more line(s) to print.')

        self.comboBox.setCurrentIndex(0)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = PrintWindow()
    sys.exit(app.exec_())

Basically it does what I want but in the terminal window. I would like a new window in my application to be created (or a TextBox in the same window, whatever) after clicking "PRINT" button with those prints statements.

I am quite new to PyQT5 so I'm looking for simplest solutions (even if they are less effective).

What would you recommend?

Upvotes: 0

Views: 1351

Answers (2)

ipaleka
ipaleka

Reputation: 3957

Create a label just like you did with label1, but set its name to be accessible on the class level naming it like self.output. I'm following your logic, but you should read about layouts (here's a nice tutorial for you):

self.output = QLabel(" ", self)
self.output.setWordWrap(True)
self.output.setGeometry(QtCore.QRect(self.output.x(), self.output.y(), self.output.width()+150, self.output.height()))
self.output.move(10, 120)    

And then instead of:

for i in range(1, x+1):
        print('That is line number: ', i, ' , ', x - i, ' more line(s) to print.')

do:

mystr = ""
for i in range(1, x+1):
    mystr += 'That is line number: ', i, ' , ', x - i, ' more line(s) to print.\n'
self.output.setText(mystr)

Upvotes: 1

MofX
MofX

Reputation: 1568

Here you go

I changed your code a bit, to reposition the button, then I inserted a QTextEdit below the button. In your print function I modified the code, to write the output into the text edit.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import pyqtSlot, QSize, QRect

class PrintWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title='Print many lines'
        self.left=10
        self.top=10
        self.width=640
        self.height=480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)

        self.statusBar().showMessage('In progress')

        label1 = QLabel("Choose how many lines would you like to print:", self)
        label1.setGeometry(QtCore.QRect(label1.x(), label1.y(), label1.width()+150, label1.height()))
        label1.move(10, 20)    

        centralWidget = QWidget(self)          
        self.setCentralWidget(centralWidget)  

        self.comboBox = QComboBox(centralWidget)
        self.comboBox.setGeometry(QRect(40, 40, 491, 31))
        self.comboBox.setObjectName(("comboBox"))
        self.comboBox.addItem("")
        self.comboBox.addItem("1")
        self.comboBox.addItem("10")
        self.comboBox.addItem("50")
        self.comboBox.addItem("100")
        self.comboBox.move(10, 60)

        button_search = QPushButton('PRINT', self)
        button_search.clicked.connect(self.PrintFunction)
        button_search.resize(200,50)
        button_search.move(220,150)  

        # create textbox
        self.textbox = QTextEdit(self)
        self.textbox.move(50, 210)
        self.textbox.resize(540, 200)
        self.textbox.setReadOnly(true);
        # create textbox done

        self.show()

    def PrintFunction(self):
        x = self.comboBox.currentText()
        if len(x) == 0:
            x = 0
        else:
            x = int(x)

        # Aggregate text and fill textbox
        data = []
        for i in range(1, x+1):
            data.append('That is line number: ' + str(i) + ' , ' + str(x - i) + ' more line(s) to print.')
        self.textbox.setText("\n".join(data))
        # Aggregate text and fill textbox done

        self.comboBox.setCurrentIndex(0)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = PrintWindow()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions