使用PyQT5和Python在窗口中打印多行

使用PyQT5和Python在窗口中打印多行,python,pyqt5,Python,Pyqt5,我试图编写一个非常简单的窗口应用程序。我想让它在窗口的某个部分打印一些“输出”(或者在新窗口中,这无关紧要) 我将尝试使用下面的代码更好地解释我的问题 import sys from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import * from PyQt5.QtCore import pyqtSlot, QSize, QRect class PrintWindow(QMainWindow): def __init

我试图编写一个非常简单的窗口应用程序。我想让它在窗口的某个部分打印一些“输出”(或者在新窗口中,这无关紧要)

我将尝试使用下面的代码更好地解释我的问题

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_())
基本上它做我想要的,但在终端窗口。我希望在我的应用程序中创建一个新窗口(或在同一窗口中创建一个文本框,无论什么),然后单击带有这些prints语句的“PRINT”按钮

我对PyQT5很陌生,所以我正在寻找最简单的解决方案(即使它们效率较低)

你有什么推荐吗?

给你

我稍微修改了代码,重新定位了按钮,然后在按钮下方插入了一个QTextEdit。 在您的打印函数中,我修改了代码,将输出写入文本编辑

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_())
给你

我稍微修改了代码,重新定位了按钮,然后在按钮下方插入了一个QTextEdit。 在您的打印函数中,我修改了代码,将输出写入文本编辑

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_())

创建一个标签,就像使用
label1
一样,但将其名称设置为可在类级别上访问,并将其命名为
self.output
。我遵循您的逻辑,但您应该阅读布局(这是为您准备的):

而不是:

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


创建一个标签,就像使用
label1
一样,但将其名称设置为可在类级别上访问,并将其命名为
self.output
。我遵循您的逻辑,但您应该阅读布局(这是为您准备的):

而不是:

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


非常感谢。我会看一看,然后询问是否需要:)我会在
PrintFunction
中使用格式化字符串,而不是字符串串联,例如
数据。追加(即行号:{I},{x-I}更多要打印的行。)
。我会做很多不同的事情,但希望尽可能少地更改,因为OP要求我这样做谢谢!我会看一看,然后询问是否需要:)我会在
PrintFunction
中使用格式化字符串,而不是字符串串联,例如
数据。追加(即行号:{I},{x-I}更多要打印的行。)
。我会做很多不同的事情,但希望按照OP的要求尽可能少地更改