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。如果您知道我的意思,您可以将此与最喜欢的网站列表进行比较。

谢谢!

4

1 回答 1

2

Here is a good PyQt Tutorial to start with.

To get a toolbar you have to create a MainWindow which will have a toolbar and contain your browser window as your central widget. To add items to the toolbar you first have to create actions and then add those actions to the toolbar. Actions can be connected with a function that will be executed when the action is triggered.

Here is a working snippet:

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_())
于 2013-08-19T21:25:41.503 回答