简体   繁体   English

PyQt5 QPushButton

[英]PyQt5 QPushButton

I want to make Pushbutton show me the text like this 1 You clicked 2 PushButton not in the same line.我想让 Pushbutton 向我显示这样的文本 1 你点击了 2 PushButton 不在同一行。

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

Class Ui_MainWindow (object):

    def setupUi(self, MainWindow): 
        MainWindow.resize(506, 312) 
        self.centralwidget = QtWidgets.QWidget(MainWindow) 
        
        # adding pushbutton 
        self.pushButton = QtWidgets.QPushButton(self.centralwidget) 
        self.pushButton.setGeometry(QtCore.QRect(200, 150, 93, 28)) 

        # adding signal and slot 
        self.pushButton.clicked.connect(self.changelabeltext) 
    
        self.label = QtWidgets.QLabel(self.centralwidget) 
        self.label.setGeometry(QtCore.QRect(140, 90, 221, 20))   

        # keeping the text of label empty before button get clicked 
        self.label.setText("")   
        
        MainWindow.setCentralWidget(self.centralwidget) 
        self.retranslateUi(MainWindow) 
        QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    def retranslateUi(self, MainWindow): 
        _translate = QtCore.QCoreApplication.translate 
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) 
        self.pushButton.setText(_translate("MainWindow", "Push Button")) 
        
    def changelabeltext(self): 

        # changing the text of label after button get clicked 
        self.label.setText("You clicked PushButton")     

        # Hiding pushbutton from the main window 
        # after button get clicked. 
        self.pushButton.hide() 

If __name__ == "__main__":
    app = QtWidgets.QApplication (sys.argv)
    
    MainWindow = QtWidgets.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 

    sys.exit(app.exec_()) 

在此处输入图像描述

在此处输入图像描述

if I understand you correctly, you want the text to be displayed as follows:如果我理解正确,您希望文本显示如下:
You clicked
PushButton
To achieve this, try placing a newline character in between the words like this:为此,请尝试在单词之间放置一个换行符,如下所示:
"You clicked \nPushButton"

Alternatively, if this does not display correctly, try adding a new label with the second half of the text to it, or increase the size of the rectangle in which you placed the text.或者,如果显示不正确,请尝试添加一个新的 label,其中包含文本的后半部分,或者增加放置文本的矩形的大小。

Although you technically only asked how to place "You clicked" below "Pushbutton" (which can be done by putting a newline in the string "You clicked \nPushButton" ), when you do this you will run into another fundamental problem with your code, mainly that you are positioning widgets by drawing them inside a QRect with a static position.尽管从技术上讲,您只询问如何将“You clicked”放在“Pushbutton”下方(可以通过在字符串"You clicked \nPushButton"中添加换行符来完成),但当您这样做时,您的代码会遇到另一个基本问题,主要是您通过在带有 static position 的QRect内绘制小部件来定位小部件。

This will produce problems when you change the text or window size, since you will have to manually change the coordinates of the QRect to place then in the correct position.当您更改文本或 window 大小时,这将产生问题,因为您必须手动更改QRect的坐标以放置在正确的 position 中。

I took the liberty of rewriting your setupUi to use layouts (which I recommend you research) instead of QRect 's.我冒昧地重写了你的setupUi以使用布局(我建议你研究)而不是QRect的。 Hope this helps, it should be a drop in replacement:希望这会有所帮助,它应该是替代品:

    def setupUi(self, MainWindow): 
        MainWindow.resize(506, 312) 
        self.mainWidget = QtWidgets.QWidget(MainWindow) 

        self.pushButton = QtWidgets.QPushButton()
        self.pushButton.clicked.connect(self.changelabeltext) 
    
        self.label = QtWidgets.QLabel() 
        self.label.setText("")   

        self.hbox = QtWidgets.QHBoxLayout()
        self.hbox.addWidget(self.pushButton)
        self.hbox.addWidget(self.label)

        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.addLayout(self.hbox)

        self.hbox.setAlignment(QtCore.Qt.AlignCenter)
        self.vbox.setAlignment(QtCore.Qt.AlignCenter)

        self.mainWidget.setLayout(self.vbox)
        MainWindow.setCentralWidget(self.mainWidget) 
        self.retranslateUi(MainWindow) 
        QtCore.QMetaObject.connectSlotsByName(MainWindow) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM