posts - 319, comments - 22, trackbacks - 0, articles - 11
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

使用PyQt 制作简单的启动界面的例子代码

Posted on 2011-05-18 07:37 RTY 阅读(2892) 评论(0)  编辑 收藏 引用 所属分类: QtPython
  1#!/usr/bin/env python
  2
  3
  4#############################################################################
  5##
  6## Copyright (C) 2010 Riverbank Computing Limited.
  7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  8## All rights reserved.
  9##
 10## This file is part of the examples of PyQt.
 11##
 12## $QT_BEGIN_LICENSE:BSD$
 13## You may use this file under the terms of the BSD license as follows:
 14##
 15## "Redistribution and use in source and binary forms, with or without
 16## modification, are permitted provided that the following conditions are
 17## met:
 18##   * Redistributions of source code must retain the above copyright
 19##     notice, this list of conditions and the following disclaimer.
 20##   * Redistributions in binary form must reproduce the above copyright
 21##     notice, this list of conditions and the following disclaimer in
 22##     the documentation and/or other materials provided with the
 23##     distribution.
 24##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 25##     the names of its contributors may be used to endorse or promote
 26##     products derived from this software without specific prior written
 27##     permission.
 28##
 29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 40## $QT_END_LICENSE$
 41##
 42#############################################################################
 43
 44
 45from PyQt4 import QtCore, QtGui
 46
 47
 48class WidgetGallery(QtGui.QDialog):
 49    def __init__(self, parent=None):
 50        super(WidgetGallery, self).__init__(parent)
 51
 52        self.originalPalette = QtGui.QApplication.palette()
 53
 54        styleComboBox = QtGui.QComboBox()
 55        styleComboBox.addItems(QtGui.QStyleFactory.keys())
 56
 57        styleLabel = QtGui.QLabel("&Style:")
 58        styleLabel.setBuddy(styleComboBox)
 59
 60        self.useStylePaletteCheckBox = QtGui.QCheckBox("&Use style's standard palette")
 61        self.useStylePaletteCheckBox.setChecked(True)
 62
 63        disableWidgetsCheckBox = QtGui.QCheckBox("&Disable widgets")
 64
 65        self.createTopLeftGroupBox()
 66        self.createTopRightGroupBox()
 67        self.createBottomLeftTabWidget()
 68        self.createBottomRightGroupBox()
 69        self.createProgressBar()
 70
 71        styleComboBox.activated[str].connect(self.changeStyle)
 72        self.useStylePaletteCheckBox.toggled.connect(self.changePalette)
 73        disableWidgetsCheckBox.toggled.connect(self.topLeftGroupBox.setDisabled)
 74        disableWidgetsCheckBox.toggled.connect(self.topRightGroupBox.setDisabled)
 75        disableWidgetsCheckBox.toggled.connect(self.bottomLeftTabWidget.setDisabled)
 76        disableWidgetsCheckBox.toggled.connect(self.bottomRightGroupBox.setDisabled)
 77
 78        topLayout = QtGui.QHBoxLayout()
 79        topLayout.addWidget(styleLabel)
 80        topLayout.addWidget(styleComboBox)
 81        topLayout.addStretch(1)
 82        topLayout.addWidget(self.useStylePaletteCheckBox)
 83        topLayout.addWidget(disableWidgetsCheckBox)
 84
 85        mainLayout = QtGui.QGridLayout()
 86        mainLayout.addLayout(topLayout, 0, 0, 12)
 87        mainLayout.addWidget(self.topLeftGroupBox, 1, 0)
 88        mainLayout.addWidget(self.topRightGroupBox, 11)
 89        mainLayout.addWidget(self.bottomLeftTabWidget, 2, 0)
 90        mainLayout.addWidget(self.bottomRightGroupBox, 21)
 91        mainLayout.addWidget(self.progressBar, 3, 0, 12)
 92        mainLayout.setRowStretch(11)
 93        mainLayout.setRowStretch(21)
 94        mainLayout.setColumnStretch(0, 1)
 95        mainLayout.setColumnStretch(11)
 96        self.setLayout(mainLayout)
 97
 98        self.setWindowTitle("Styles")
 99        self.changeStyle('Windows')
100
101    def changeStyle(self, styleName):
102        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(styleName))
103        self.changePalette()
104
105    def changePalette(self):
106        if (self.useStylePaletteCheckBox.isChecked()):
107            QtGui.QApplication.setPalette(QtGui.QApplication.style().standardPalette())
108        else:
109            QtGui.QApplication.setPalette(self.originalPalette)
110
111    def advanceProgressBar(self):
112        curVal = self.progressBar.value()
113        maxVal = self.progressBar.maximum()
114        self.progressBar.setValue(curVal + (maxVal - curVal) / 100)
115
116    def createTopLeftGroupBox(self):
117        self.topLeftGroupBox = QtGui.QGroupBox("Group 1")
118
119        radioButton1 = QtGui.QRadioButton("Radio button 1")
120        radioButton2 = QtGui.QRadioButton("Radio button 2")
121        radioButton3 = QtGui.QRadioButton("Radio button 3")
122        radioButton1.setChecked(True)
123
124        checkBox = QtGui.QCheckBox("Tri-state check box")
125        checkBox.setTristate(True)
126        checkBox.setCheckState(QtCore.Qt.PartiallyChecked)
127
128        layout = QtGui.QVBoxLayout()
129        layout.addWidget(radioButton1)
130        layout.addWidget(radioButton2)
131        layout.addWidget(radioButton3)
132        layout.addWidget(checkBox)
133        layout.addStretch(1)
134        self.topLeftGroupBox.setLayout(layout)    
135
136    def createTopRightGroupBox(self):
137        self.topRightGroupBox = QtGui.QGroupBox("Group 2")
138
139        defaultPushButton = QtGui.QPushButton("Default Push Button")
140        defaultPushButton.setDefault(True)
141
142        togglePushButton = QtGui.QPushButton("Toggle Push Button")
143        togglePushButton.setCheckable(True)
144        togglePushButton.setChecked(True)
145
146        flatPushButton = QtGui.QPushButton("Flat Push Button")
147        flatPushButton.setFlat(True)
148
149        layout = QtGui.QVBoxLayout()
150        layout.addWidget(defaultPushButton)
151        layout.addWidget(togglePushButton)
152        layout.addWidget(flatPushButton)
153        layout.addStretch(1)
154        self.topRightGroupBox.setLayout(layout)
155
156    def createBottomLeftTabWidget(self):
157        self.bottomLeftTabWidget = QtGui.QTabWidget()
158        self.bottomLeftTabWidget.setSizePolicy(QtGui.QSizePolicy.Preferred,
159                                               QtGui.QSizePolicy.Ignored)
160
161        tab1 = QtGui.QWidget()
162        tableWidget = QtGui.QTableWidget(1010)
163
164        tab1hbox = QtGui.QHBoxLayout()
165        tab1hbox.setMargin(5)
166        tab1hbox.addWidget(tableWidget)
167        tab1.setLayout(tab1hbox)
168
169        tab2 = QtGui.QWidget()
170        textEdit = QtGui.QTextEdit()
171
172        textEdit.setPlainText("Twinkle, twinkle, little star,\n"
173                              "How I wonder what you are.\n" 
174                              "Up above the world so high,\n"
175                              "Like a diamond in the sky.\n"
176                              "Twinkle, twinkle, little star,\n" 
177                              "How I wonder what you are!\n")
178
179        tab2hbox = QtGui.QHBoxLayout()
180        tab2hbox.setMargin(5)
181        tab2hbox.addWidget(textEdit)
182        tab2.setLayout(tab2hbox)
183
184        self.bottomLeftTabWidget.addTab(tab1, "&Table")
185        self.bottomLeftTabWidget.addTab(tab2, "Text &Edit")
186
187    def createBottomRightGroupBox(self):
188        self.bottomRightGroupBox = QtGui.QGroupBox("Group 3")
189        self.bottomRightGroupBox.setCheckable(True)
190        self.bottomRightGroupBox.setChecked(True)
191
192        lineEdit = QtGui.QLineEdit('s3cRe7')
193        lineEdit.setEchoMode(QtGui.QLineEdit.Password)
194
195        spinBox = QtGui.QSpinBox(self.bottomRightGroupBox)
196        spinBox.setValue(50)
197
198        dateTimeEdit = QtGui.QDateTimeEdit(self.bottomRightGroupBox)
199        dateTimeEdit.setDateTime(QtCore.QDateTime.currentDateTime())
200
201        slider = QtGui.QSlider(QtCore.Qt.Horizontal, self.bottomRightGroupBox)
202        slider.setValue(40)
203
204        scrollBar = QtGui.QScrollBar(QtCore.Qt.Horizontal,
205                self.bottomRightGroupBox)
206        scrollBar.setValue(60)
207
208        dial = QtGui.QDial(self.bottomRightGroupBox)
209        dial.setValue(30)
210        dial.setNotchesVisible(True)
211
212        layout = QtGui.QGridLayout()
213        layout.addWidget(lineEdit, 0, 0, 12)
214        layout.addWidget(spinBox, 1, 0, 12)
215        layout.addWidget(dateTimeEdit, 2, 0, 12)
216        layout.addWidget(slider, 3, 0)
217        layout.addWidget(scrollBar, 4, 0)
218        layout.addWidget(dial, 3121)
219        layout.setRowStretch(51)
220        self.bottomRightGroupBox.setLayout(layout)
221
222    def createProgressBar(self):
223        self.progressBar = QtGui.QProgressBar()
224        self.progressBar.setRange(0, 10000)
225        self.progressBar.setValue(0)
226
227        timer = QtCore.QTimer(self)
228        timer.timeout.connect(self.advanceProgressBar)
229        timer.start(1000)
230
231    def getWidget(self, splash):
232        t = QtCore.QElapsedTimer()
233        t.start()
234        while (t.elapsed() < 5000):
235            str = QtCore.QString("times = "+ QtCore.QString.number(t.elapsed())
236            splash.showMessage(str)
237            QtCore.QCoreApplication.processEvents()
238
239if __name__ == '__main__':
240
241    import sys
242
243    app = QtGui.QApplication(sys.argv)
244
245    #splash
246    pixmap = QtGui.QPixmap(u"C:\\Users\\anlin\\Pictures\\13.png")
247    splash = QtGui.QSplashScreen(pixmap)
248    label = QtGui.QLabel(splash)
249    label.setText("<br><br>Foxreal")
250    label.setAlignment(QtCore.Qt.AlignRight)
251    splash.show()
252    QtCore.QCoreApplication.processEvents()
253
254    #main window
255    gallery = WidgetGallery()
256    splash.finish(gallery.getWidget(splash))
257    gallery.show()
258    sys.exit(app.exec_()) 
259

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理