2013-08-19 36 views
1
#!/usr/bin/env python 
#-*- coding:utf-8 -*- 
import sys 
from PySide.QtCore import * 
from PySide.QtGui import * 
from PySide.QtWebKit import * 
from PySide.QtHelp import * 
from PySide.QtNetwork import * 

app = QApplication(sys.argv) 

web = QWebView() 
web.load(QUrl("http://google.com")) 
web.show() 
web.resize(650, 750) 
q_pixmap = QPixmap('icon.ico') 
q_icon = QIcon(q_pixmap) 
QApplication.setWindowIcon(q_icon) 
web.setWindowTitle('Browser') 
sys.exit(app.exec_()) 

如何添加一个工具栏上有两个按钮: 一个叫'URL 1',另一个'URL 2'因此,如果他们点击它将打开一个URL。如果你知道我的意思,YOu可以将这个与喜欢的网站列表进行比较。如何在PySide的浏览器示例上添加工具栏?

谢谢!

+0

那么不是真的,我没有成功,使一个工具栏。但我希望工具栏中的按钮可以在同一窗口中重新加载其他页面。这样它会重定向到其他网站。我怎么做? – svenweer

+0

我更新了答案。现在检查出来。您只需将另一个URL加载到与'QAction'连接的回调函数中的Web浏览器中即可。 –

+0

非常感谢! – svenweer

回答

2

这是一个很好的PyQt Tutorial开始。

要得到一个工具栏,你必须创建一个MainWindow,它将有一个工具栏,并包含你的浏览器窗口作为你的中心控件。要将项目添加到工具栏,首先必须创建操作,然后将这些操作添加到工具栏。动作可以与一个将在触发动作时执行的函数连接。

这里是一个工作片段:

import sys 
from PySide import QtCore, QtGui, QtWebKit 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self): 
     super(MainWindow, self).__init__() 
     # Create an exit action 
     exitAction = QtGui.QAction('Load Yahoo', self) 
     # Optionally you can assign an icon to the action 
     # exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self) 
     exitAction.setShortcut('Ctrl+Q') # set the shortcut 
     # Connect the action with a custom function 
     exitAction.triggered.connect(self.load_yahoo) 
     # Create the toolbar and add the action 
     self.toolbar = self.addToolBar('Exit') 
     self.toolbar.addAction(exitAction) 

     # Setup the size and title of the main window 
     self.resize(650, 750) 
     self.setWindowTitle('Browser') 

     # Create the web widget and set it as the central widget. 
     self.web = QtWebKit.QWebView(self) 
     self.web.load(QtCore.QUrl('http://google.com')) 
     self.setCentralWidget(self.web) 

    def load_yahoo(self): 
     self.web.load(QtCore.QUrl('http://yahoo.com')) 


app = QtGui.QApplication(sys.argv) 
main_window = MainWindow() 
main_window.show()  
sys.exit(app.exec_())