เนื่องจากเราใช้ PySide ซึ่งอิง Qt อีกทอดหนึ่ง ดังนั้น web browser engine ที่ใช้งานคือ WebKit ครับ สำหรับการใช้ QWebView เราต้อง from PySide.QtWebKit import * ด้วยนะครับ
การดึงหน้าเว็บเพจโดยอ้างอิงที่อยู่เว็บเพจมาแสดง
มีการใช้งานดังนี้ครับweb.load(QUrl("ที่อยู่เว็บไซต์"))
ตัวอย่างที่ 5
import sys from PySide.QtCore import * from PySide.QtGui import * from PySide.QtWebKit import * app = QApplication(sys.argv) web = QWebView() #กำหนดตัวแปร web ใช้ QWebView web.load(QUrl("http://www.google.co.th")) #ดึงหน้าเว็บเพจมาแสดง web.show() sys.exit(app.exec_())
ผลลัพธ์
web_view.setHtml('''HTML CODE''')
ตัวอย่างที่ 6
import sys from PySide.QtCore import * from PySide.QtGui import * from PySide.QtWebKit import * app = QApplication(sys.argv) web = QWebView() web.setHtml('''<html> <head> <title>ทดสอบ</title> </head> <body> <h1>Hello, World!</h1> <hr /> ทดสอบการแสดงผล HTML ใน QWebView </body> </html>''') web.show() sys.exit(app.exec_())
ผลลัพธ์
ตัวอย่างที่ 7
import sys from PySide.QtCore import * from PySide.QtGui import * from PySide.QtWebKit import * app = QApplication([]) win = QWebView() img = open('101_0336.png', 'rb').read() #ดึงไฟล์ 101_0336.png เข้ามาแล้วอ่าน win.setContent(img,"image/png") #ดึงตัวแปร img ที่เก็บไฟล์รูปภาพมาแสดง โดยมีการกำหนด setContent เป็น image/png win.show() app.exec_()ผลลัพธ์
ขอบคุณครับ
0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)