如何使用 PyQt5 在编辑器旁边打印文本

How to print text beside Editor using PyQt5

我刚开始学习如何使用 PyQt5 构建 GUI。 我在 Internet 上追踪一些示例并尝试创建一个 GUI 用于练习。 但是当我试图在编辑器旁边显示文本时出现问题。 我按照我在互联网上找到的方式进行操作,但它就是行不通。 谁能告诉我如何解决它?

我评论了我试图在我的代码中显示文本的部分

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import xml.etree.cElementTree as ET

class App(QMainWindow):

def __init__(self):
    super().__init__()
    self.title = 'PyQt5 textbox - pythonspot.com'
    self.left = 10
    self.top = 10
    self.width = 400
    self.height = 200
    self.initUI()

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

    # Create textbox
    self.textbox = QLineEdit(self)
    #self.textbox.setAlignment(Qt.AlignRight)
    self.textbox.move(80, 20)
    self.textbox.resize(200,40)


    self.textbox2 = QLineEdit(self)
    self.textbox2.move(80, 80)
    self.textbox2.resize(200,40)        



    #####################################
    # the part i am trying to show text #
    #####################################  
    txt1 = QLabel("case indes",self)
    txt1.setAlignment(Qt.AlignCenter)

    mytext = QFormLayout()
    mytext.addRow(txt1,self.textbox) # not showing in Aligned position 
    mytext.addRow("Case type",tbox2) # not working




    # Create a button in the window
    self.button = QPushButton('Show text', self)
    self.button.move(20,150)


    # connect button to function on_click
    self.button.clicked.connect(self.on_click)
    self.center()
    self.show()

@pyqtSlot()
def on_click(self):
    textboxValue = self.textbox.text()
    textboxValue2 = self.textbox2.text()
    QMessageBox.question(self, 'Message - pythonspot.com', "You typed: " + textboxValue + " , second msg is: " + textboxValue2, QMessageBox.Ok, QMessageBox.Ok)
    print(textboxValue)
    self.textbox.setText("")
    self.textbox2.setText("")

def center(self):  
    qr = self.frameGeometry()
    cp = QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())        

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

布局用于管理小部件的位置和大小,因此您不应该使用移动或调整大小,您从未确定布局属于哪个小部件,最后 QMainWindow 是一个特殊的小部件,您必须在其中建立一个中央小部件。在下一节中有解决方案:

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

    central_widget = QWidget()
    self.setCentralWidget(central_widget)

    # Create textbox
    self.textbox = QLineEdit()
    self.textbox2 = QLineEdit()   

    txt1 = QLabel("case indes",self)
    txt1.setAlignment(Qt.AlignCenter)    

    mytext = QFormLayout(central_widget)
    mytext.addRow(txt1, self.textbox) # not showing in Aligned position 
    mytext.addRow("Case type", self.textbox2) # not working

    # Create a button in the window
    self.button = QPushButton('Show text')
    mytext.addRow(self.button)
    # connect button to function on_click
    self.button.clicked.connect(self.on_click)
    self.center()
    self.show()