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

时间:2019-02-20 07:27:38

标签: python python-3.x pyqt pyqt5 qformlayout

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

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

enter image description here

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

1 个答案:

答案 0 :(得分:0)

布局用于管理窗口小部件的位置和大小,因此您不应使用移动或调整大小,您从未确定布局所属的窗口小部件,最后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()