最近我了解了PyQt4 Designer并决定尝试一下,下面是代码。
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def back(self):
self.webView.back()
def forword(self):
self.webView.forward()
def reload(self):
self.webView.reload()
def home(self):
self.webView.load(QtCore.QUrl("http://www.google.com"))
def search(self):
url = self.ln_addressbar.text()
self.webView.load(QtCore.QUrl(url))
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(770, 475)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tb_back = QtGui.QToolButton(self.centralwidget)
self.tb_back.setGeometry(QtCore.QRect(10, 20, 41, 31))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("icons/back.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_back.setIcon(icon)
self.tb_back.setIconSize(QtCore.QSize(32, 32))
self.tb_back.setObjectName(_fromUtf8("tb_back"))
############### Event #############################3
self.tb_back.clicked.connect(self.back)
##################################################
self.tb_reload = QtGui.QToolButton(self.centralwidget)
self.tb_reload.setGeometry(QtCore.QRect(60, 20, 41, 31))
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("icons/reload.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_reload.setIcon(icon1)
self.tb_reload.setIconSize(QtCore.QSize(32, 32))
self.tb_reload.setObjectName(_fromUtf8("tb_reload"))
############### Event #############################3
self.tb_reload.clicked.connect(self.reload)
##################################################
self.tb_forword_2 = QtGui.QToolButton(self.centralwidget)
self.tb_forword_2.setGeometry(QtCore.QRect(110, 20, 41, 31))
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(_fromUtf8("icons/forword.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_forword_2.setIcon(icon2)
self.tb_forword_2.setIconSize(QtCore.QSize(32, 32))
self.tb_forword_2.setObjectName(_fromUtf8("tb_forword_2"))
############### Event #############################3
self.tb_forword_2.clicked.connect(self.forword)
##################################################
self.tb_home = QtGui.QToolButton(self.centralwidget)
self.tb_home.setGeometry(QtCore.QRect(160, 20, 41, 31))
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap(_fromUtf8("icons/home.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_home.setIcon(icon3)
self.tb_home.setIconSize(QtCore.QSize(32, 32))
self.tb_home.setObjectName(_fromUtf8("tb_home"))
############### Event #############################3
self.tb_home.clicked.connect(self.home)
##################################################
self.ln_addressbar = QtGui.QLineEdit(self.centralwidget)
self.ln_addressbar.setGeometry(QtCore.QRect(210, 20, 491, 31))
self.ln_addressbar.setObjectName(_fromUtf8("ln_addressbar"))
self.tb_search = QtGui.QToolButton(self.centralwidget)
self.tb_search.setGeometry(QtCore.QRect(710, 20, 41, 31))
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap(_fromUtf8("icons/search.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_search.setIcon(icon4)
self.tb_search.setIconSize(QtCore.QSize(32, 32))
self.tb_search.setObjectName(_fromUtf8("tb_search"))
############### Event #############################3
self.tb_search.clicked.connect(self.search)
##################################################
self.webView = QtWebKit.QWebView(self.centralwidget)
self.webView.setGeometry(QtCore.QRect(0, 60, 771, 421))
self.webView.setUrl(QtCore.QUrl(_fromUtf8("about:blank")))
self.webView.setObjectName(_fromUtf8("webView"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.tb_back.setText(_translate("MainWindow", "...", None))
self.tb_reload.setText(_translate("MainWindow", "...", None))
self.tb_forword_2.setText(_translate("MainWindow", "...", None))
self.tb_home.setText(_translate("MainWindow", "...", None))
self.tb_search.setText(_translate("MainWindow", "...", None))
from PyQt4 import QtWebKit
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
但是,当我尝试运行它时,将.ui
文件转换为.py
后,我收到此错误:
File "C:\Users\###-_000\Desktop\browser.py", line 3, in <module>
from PyQt4 import QtCore, QtGui
ImportError: cannot import name 'QtCore'
提前致谢!