使用pycharm+pyqt5 觸發自定義方法


1、依舊使用上次QtDesigner做的界面,如下圖:


 

2、本次的使用text Browser 和 text Edit 和 pushButton控件做觸發聯系:

 

3、目的實現在text Edit中隨意填寫內容,點擊pushButton后將內容添加到text Browser中進行顯示,效果如下:

 

4、代碼分析:

 

 
import sys
from blog import Ui_MainWindow
from PyQt5 import QtWidgets


class mywindow(QtWidgets.QWidget, Ui_MainWindow):
    global mStr
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)
        self.pushButton_3.clicked.connect(self.addText)

    
    def getText(self):
        global mStr
        mStr = self.textEdit.toHtml()

    def setText(self):
        global mStr
        self.textBrowser.append(mStr)

    def addText(self):
        self.getText()
        self.setText()

if __name__=="__main__":

    app=QtWidgets.QApplication(sys.argv)
    myshow=mywindow()
    myshow.show()
    sys.exit(app.exec_())

 

 

 

利用class UI類,將方法寫入class中,利用textEdit的toHtml屬性將編輯框中的文本內容取出來放到全局變量mStr中,見def getTest(self):,再利用textBrowser的append方法把mStr的數據添加進去,見setText(self):,最后利用pushButton的clicked.connect()方法,把addText操作關聯即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM