#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we receive data from
a QtGui.QInputDialog dialog. author: Jan Bodnar
website: zetcode.com
last edited: October 2011
""" import sys
from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): self.btn = QtGui.QPushButton('Dialog', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog) self.le = QtGui.QLineEdit(self)
self.le.move(130, 22) self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Input dialog')
self.show() def showDialog(self): # This line displays the input dialog. The first string is a dialog title, the second one is a message within the dialog. The dialog returns the entered text and a boolean value. If we click the Ok button, the boolean value is true.
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
'Enter your name:') # The text that we have received from the dialog is set to the line edit widget.
if ok:
self.le.setText(str(text)) def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main() -------------------------------------------------------------------------------- #!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we select a colour value
from the QtGui.QColorDialog and change the background
colour of a QtGui.QFrame widget. author: Jan Bodnar
website: zetcode.com
last edited: October 2011
""" import sys
from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): # This is an initial colour of the QtGui.QFrame background.
col = QtGui.QColor(0, 0, 0) self.btn = QtGui.QPushButton('Dialog', self)
self.btn.move(20, 20) self.btn.clicked.connect(self.showDialog) self.frm = QtGui.QFrame(self)
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
self.frm.setGeometry(130, 22, 100, 100) self.setGeometry(300, 300, 250, 180)
self.setWindowTitle('Color dialog')
self.show() def showDialog(self): # This line will pop up the QtGui.QColorDialog.
col = QtGui.QColorDialog.getColor() # We check if the colour is valid. If we click on the Cancel button, no valid colour is returned. If the colour is valid, we change the background colour using style sheets.
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name()) def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main() -------------------------------------------------------------------------------- #!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we select a font name
and change the font of a label. author: Jan Bodnar
website: zetcode.com
last edited: October 2011
""" import sys
from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): vbox = QtGui.QVBoxLayout() btn = QtGui.QPushButton('Dialog', self)
btn.setSizePolicy(QtGui.QSizePolicy.Fixed,
QtGui.QSizePolicy.Fixed) btn.move(20, 20) vbox.addWidget(btn) btn.clicked.connect(self.showDialog) self.lbl = QtGui.QLabel('Knowledge only matters', self)
self.lbl.move(130, 20) vbox.addWidget(self.lbl)
self.setLayout(vbox) self.setGeometry(300, 300, 250, 180)
self.setWindowTitle('Font dialog')
self.show() def showDialog(self): # Here we pop up the font dialog. The getFont() method returns the font name and the ok parameter. It is equal to True if the user clicked OK; otherwise it is False.
font, ok = QtGui.QFontDialog.getFont() # If we clicked ok, the font of the label would be changed.
if ok:
self.lbl.setFont(font) def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main() -------------------------------------------------------------------------------- #!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we select a file with a
QtGui.QFileDialog and display its contents
in a QtGui.QTextEdit. author: Jan Bodnar
website: zetcode.com
last edited: October 2011
""" import sys
from PyQt4 import QtGui #The example is based on the QtGui.QMainWindow widget because we centrally set the text edit widget.
class Example(QtGui.QMainWindow): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): self.textEdit = QtGui.QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar() openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open new File')
openFile.triggered.connect(self.showDialog) menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile) self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('File dialog')
self.show() def showDialog(self): # We pop up the QtGui.QFileDialog. The first string in the getOpenFileName() method is the caption. The second string specifies the dialog working directory. By default, the file filter is set to All files (*).
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'/home') # The selected file name is read and the contents of the file are set to the text edit widget.
f = open(fname, 'r') with f:
data = f.read()
self.textEdit.setText(data) def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

ZetCode PyQt4 tutorial Dialogs的更多相关文章

  1. ZetCode PyQt4 tutorial Drag and Drop

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...

  2. ZetCode PyQt4 tutorial widgets II

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  3. ZetCode PyQt4 tutorial widgets I

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  4. ZetCode PyQt4 tutorial signals and slots

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  5. ZetCode PyQt4 tutorial layout management

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows ...

  6. ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This program create ...

  7. ZetCode PyQt4 tutorial First programs

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  8. ZetCode PyQt4 tutorial custom widget

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  9. ZetCode PyQt4 tutorial basic painting

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

随机推荐

  1. 文件下载—SSM框架文件下载

    1.准备上传下载的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>common ...

  2. linux wa%过高,iostat查看io状况

    命令总结: 1. top/vmstat 发现 wa%过高,vmstat b >1: 参考文章: 1. 关于Linux系统指令 top 之 %wa 占用高,用`iostat`探个究竟 最近测试一项 ...

  3. mongodb-的副本集

    复制的重要性不再多说,其主要就是提供数据保护,数据高可用和灾难恢复. 复制是跨多个mongodb服务器分布和维护的方法.mongodb可以把数据从一个节点复制到其他节点并在修改时进行同步. mongo ...

  4. php-fpm开启报错-ERROR: An another FPM instance seems to already listen on /tmp/php-cgi.sock

    在升级了php7.2.0版本之后,重新启动php-fpm过程中遇到一个报错. An another FPM instance seems to already listen on /tmp/php-c ...

  5. @Tranactional事务没有回滚

    一.特性 先来了解一下@Transactional注解事务的特性吧,可以更好排查问题 1.service类标签(一般不建议在接口上)上添加@Transactional,可以将整个类纳入spring事务 ...

  6. 20145311 《Java程序设计》第5周学习总结

    20145311 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 8.1语法与继承结构 8.1.1Try.catch java中所有的错误都会打包为对象,可以try catc ...

  7. 20172305 2018-2019-1 《Java软件结构与数据结构》第三周学习总结

    20172305 2018-2019-1 <Java软件结构与数据结构>第三周学习总结 教材学习内容总结 本周内容主要为书第五章内容: 队列 线性集合(元素从一端加入,另一端删除) 先进先 ...

  8. 【web】支持jsp+mvc访问

    直接使用SpringMVC时配置访问jsp页面时很容易的事,但是由于spring Boot使用内嵌的servlet容器,所以对jsp的支持不是很好,而且也不建议使用jsp,但是为了满足这种返回jsp页 ...

  9. UVa 1637 纸牌游戏(全概率公式)

    https://vjudge.net/problem/UVA-1637 题意: 36张牌分成9堆,每堆4张牌.每次可以拿走某两堆顶部的牌,但需要点数相同.每种拿法的概率均为1/5.求成功概率. 思路: ...

  10. BZOJ 2226 【SPOJ 5971】 LCMSum

    题目链接:LCMSum 这个题显然就是要我们推式子了……那么就来推一波: \begin{aligned}&\sum_{i=1}^n lcm(i,n) \\=&\sum_{i=1}^n\ ...