我正在用PyQt5中的搜索框制作一个简单的浏览器。这是我写的:
import PyQt5
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QMainWindow
from PyQt5.QtWebKitWidgets import QWebView , QWebPage
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtNetwork import *
import sys
from optparse import OptionParser
class App(QMainWindow):
def initiate(self):
super().initiate()
self.InitUI()
def initUI(self):
self.setWindowTitle('Browser')
self.setGeometry(100, 200, 1000, 2000)
self.searchbox = QLineEdit(self)
self.searchbox.move(20, 20)
self.searchbox.resize(1500,40)
self.go = QPushButton('Go', self)
self.go.move(1810, 20)
self.go.connect(self.gourl)
self.show()
def gourl(self):
url = self.searchbox.text()
class Browser(QWebView):
def __init__(self):
self.view = QWebView.__init__(self)
self.setWindowTitle('Loading...')
self.titleChanged.connect(self.adjustTitle)
def load(self,url):
self.setUrl(QUrl(url))
App.searchbox.setText(url)
def adjustTitle(self):
self.setWindowTitle(self.title())
app = QApplication(sys.argv)
view = Browser()
box = App()
box.show()
view.show()
view.load("https://duckduckgo.com")
app.exec_()
浏览器部分已加载,但文本框未显示。 Python也抛出此错误:
Traceback (most recent call last):
File "C:\Users\Sid\Desktop\browser.py", line 43, in <module>
view.load("https://duckduckgo.com")
File "C:\Users\Sid\Desktop\browser.py", line 34, in load
App.searchbox.setText(url)
AttributeError: type object 'App' has no attribute 'searchbox'
我不知道为什么文本框不显示,我也不明白为什么会引发错误。有人可以指出错误吗? 提前致谢。 :)
答案 0 :(得分:2)
Qt 5.6的新增功能 https://doc.qt.io/qt-5/whatsnew56.html#removed-functionality
从QtWebKit移植到QtWebEngine https://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html
import sys
#import PyQt5
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QMainWindow
#from PyQt5.QtWebKitWidgets import QWebView , QWebPage
#from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebEngine import *
from PyQt5.QtWebEngineWidgets import *
#from PyQt5.QtNetwork import *
#from optparse import OptionParser
class App(QMainWindow):
# def initiate(self):
# super().initiate()
def __init__(self, parent=None):
super().__init__(parent)
self.initUI() # - InitUI -> + initUI
def initUI(self):
self.setWindowTitle('Browser')
self.setGeometry(100, 200, 500, 400)
self.searchbox = QLineEdit("https://stackoverflow.com/questions/57841281/not-sure-why-textbox-isnt-showing", self)
self.searchbox.move(20, 20)
self.searchbox.resize(460,40)
self.go = QPushButton('Go', self)
self.go.move(370, 100)
self.go.clicked.connect(self.gourl) # clicked
self.show()
def gourl(self):
url = self.searchbox.text()
print(f"url = {url}")
self.webview = Browser()
self.webview.load(QUrl(url))
self.webview.show()
class Browser(QWebEngineView): #(QWebView):
windowList = []
def createWindow(self, QWebEnginePage_WebWindowType):
new_webview = Browser()
new_window = App()
new_window.setCentralWidget(new_webview)
#new_window.show()
self.windowList.append(new_window)
return new_webview
"""
def __init__(self, parent=None):
super().__init__(parent)
# self.view = QWebView.__init__(self)
self.setWindowTitle('Loading...')
self.titleChanged.connect(self.adjustTitle)
def load(self,url):
self.setUrl(QUrl(url))
App.searchbox.setText(url)
def adjustTitle(self):
self.setWindowTitle(self.title())
"""
if __name__ == "__main__":
app = QApplication(sys.argv)
# view = Browser()
box = App()
box.show()
# view.show()
# view.load("https://duckduckgo.com")
sys.exit(app.exec_())
更新
没有新窗口??如果可能的话
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QAction, QLineEdit,
QMessageBox, QMainWindow, QGridLayout)
from PyQt5.QtWebEngine import *
from PyQt5.QtWebEngineWidgets import *
class App(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.searchbox = QLineEdit("https://stackoverflow.com/questions/57841281/not-sure-why-textbox-isnt-showing", self)
self.go = QPushButton('Go', self)
self.go.clicked.connect(self.gourl)
self.webview = Browser()
self.grid = QGridLayout(centralWidget)
self.grid.addWidget(self.webview, 0, 0, 1, 2)
self.grid.addWidget(self.searchbox, 1, 0)
self.grid.addWidget(self.go, 1, 1)
def gourl(self):
url = self.searchbox.text()
self.webview.load(QUrl(url))
class Browser(QWebEngineView): #(QWebView):
windowList = []
def createWindow(self, QWebEnginePage_WebWindowType):
new_webview = Browser()
new_window = App()
new_window.setCentralWidget(new_webview)
#new_window.show()
self.windowList.append(new_window)
return new_webview
if __name__ == "__main__":
app = QApplication(sys.argv)
box = App()
box.setWindowTitle('Browser')
box.resize(600, 500)
box.show()
sys.exit(app.exec_())