How to set the size of browser using QtWebKit

前端 未结 1 1060
-上瘾入骨i
-上瘾入骨i 2021-02-10 14:41

Recently I use PyQt4 to crawl some web pages. I want to set different size of the browser which is generated by QWebView().show(). I tried to use the following code

相关标签:
1条回答
  • 2021-02-10 14:45

    I tested this, the window resizes itself after loadFinished is emitted:

    import sys 
    from PySide.QtCore import QUrl, QSize
    from PySide.QtGui import QApplication, QMainWindow, QWidget
    from PySide.QtWebKit import QWebView
    
    
    class Browser(QMainWindow):
    
        def __init__(self):
            QMainWindow.__init__(self)
            self.resize(300, 300)
            self.web_view = QWebView()
            self.setCentralWidget(self.web_view)
    
            self.web_view.loadFinished.connect(self._load_finished)
    
        def _load_finished(self):
            frame = self.web_view.page().mainFrame()
            self.web_view.page().setViewportSize(frame.contentsSize())
            self.resize(frame.contentsSize())
            html_data = frame.toHtml()
    
    
    if __name__ == '__main__': 
        app = QApplication(sys.argv) 
        browser = Browser() 
        r = QUrl("http://www.yahoo.com")
        browser.web_view.load(r)
        browser.show()
        app.exec_()
    
    0 讨论(0)
提交回复
热议问题