如何在PySide的浏览器示例中添加工具栏?

时间:2013-08-19 19:54:42

标签: python url browser toolbar pyside

#!/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”。因此,如果他们点击它,它将打开一个网址。如果你知道我的意思,你可以将它与喜欢的网站列表进行比较。

谢谢!

1 个答案:

答案 0 :(得分: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_())