PyQt4---QTextBrowser

browser = QTextBrowser() #實例化一個textbrowser

browser.append(‘sdfsdfds’) #追加內容

browser.setOpenLinks(True) #打開文檔內部鏈接 默認爲True

browser.setOpenExternalLinks(True) #打開外部鏈接 默認false 當openlinks設置false時 該選項無效

textbrowser.setSearchPaths([“ldks”,":/sdfs"]) #設置文檔搜索路徑 參數爲包含目錄的List

textbrowser.setSource(“index.html”) #設置文檔

dt=textbrowser.documentTitle() #返回文檔的標題

self.connect(textbrowser,SIGNAL(“SourceChanged(QUrl)”),self.update) #發出一個SourceChanged(QUrl)信號

textbrowser同時 具有以下插槽: home() :返回主文檔, backward() #返回上一文檔,forward()前進

browser.setDocumentTitle(‘dsds’) #設置文檔標題
例子:

# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Form(QDialog):
  
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()
        self.lineedit = QLineEdit("Type an expression and press Enter")
        #self.lineedit.selectAll()
        layout = QVBoxLayout() #垂直盒式佈局
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        #layout = QGridLayout() #網格佈局
        #layout.addWidget(self.browser,0, 0) 
        #layout.addWidget(self.lineedit,0, 0)
        self.setLayout(layout)
        #self.lineedit.setFocus()
        self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi) #信號綁定到槽,按回車後執行槽函數
        self.setWindowTitle("Calculate")
  
    def updateUi(self):
        try:
            text = unicode(self.lineedit.text())
            self.browser.append("%s = <b>%s</b>" % (text, eval(text)))   #顯示內容支撐html格式語法,eval返回表達式結果
        except:
            self.browser.append(
                    "<font color=red>%s is invalid!</font>" % text)
  
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

程序效果如下:
在這裏插入圖片描述
該程序通過QTextBrowser控件可實現在下方輸入一個表達式,然後在上方顯示出來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章