ZetCode PyQt4 tutorial Dialogs
#!/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的更多相关文章
- ZetCode PyQt4 tutorial Drag and Drop
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...
- ZetCode PyQt4 tutorial widgets II
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial widgets I
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial signals and slots
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial layout management
!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows ...
- 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 ...
- ZetCode PyQt4 tutorial First programs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial custom widget
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial basic painting
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
随机推荐
- VS2010/MFC编程入门之七(对话框:为对话框添加控件)
创建对话框资源需要创建对话框模板.修改对话框属性.为对话框添加各种控件等步骤,前面一讲中鸡啄米已经讲了创建对话框模板和修改对话框属性,本节继续讲如何为对话框添加控件. 上一讲中鸡啄米创建了一个名为“A ...
- Transactions and beyond it..
While data integrity is managed very effectively within a single database with row locking, deadlock ...
- Fiddler 手机无法上网问题
一.Fiddler版本升级后需要升级netframework 二.每一个域名点击需要授权 出现此页面依次点击显示详细信息和访问此网站. 有时候网站白板,可能是cdn域名没有授权的原因,可以直接在url ...
- Ubuntu Server VS Ubuntu Desktop区别
今天有位朋友问我,Ubuntu Server 与 Ubuntu Desktop的区别在哪里!区别如下: SERVER没有GUI SERVER没有一堆的桌面软件 SERVER在编译时使用的参数不一样,会 ...
- 如何把本地git仓库托管到码云上
提交代码到本地git仓库 git init git status git add . git status git commit -m "init my project" ...
- 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror
下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...
- MongoDB 默认写入关注保存数据丢失问题与源码简单分析
MongoDB 默认写入关注可能保存数据丢失问题分析 问题描述: EDI服务进行优化,将原有MQ发送成功并且DB写入成功,两个条件都达成,响应接收订单数据成功,修改为只有有一个条件成功就响应接收数据成 ...
- ZOJ 3541 The Last Puzzle(经典区间dp)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3541 题意:有一排开关,有个开关有两个值t和d,t是按下开关后在t秒后会自 ...
- Windows__书
1.<<Windows 网络与通信程序设计>> (第2版) 2. 3.
- mysql之innodb的锁分类介绍
一.innodb行锁分类 record lock:记录锁,也就是仅仅锁着单独的一行 gap lock:区间锁,仅仅锁住一个区间(注意这里的区间都是开区间,也就是不包括边界值. next-key loc ...