PySide——Python图形化界面入门教程(三)
PySide——Python图形化界面入门教程(三)
——使用内建新号和槽
——Using Built-In Signals and Slots
上一个教程中,我们学习了如何创建和建立交互widgets,以及将他们布局的两种不同的方法。今天我们继续讨论Python/Qt应用响应用户触发的事件:信号和槽。
当用户执行一个动作——点击按钮,选择组合框的值,在文本框中打字——这个widget就会发出一个信号。这个信号自己什么都不做,它必须和槽连接起来才行。槽是一个接受信号的执行动作的对象。
连接内建PySide/PyQt信号
Qt widgets有许多的内建信号。例如,当QPushButton被点击的时候,它发出它的clicked信号。clicked信号可以被连接到一个拥有槽功能的函数(只是一个概要,需要更多内容去运行)
@Slot()
def clicked_slot():
''' This is called when the button is clicked. '''
print('Ouch!') # Create the button
btn = QPushButton('Sample') # Connect its clicked signal to our slot
btn.clicked.connect(clicked_slot)
注意@Slot()装饰(decorator)在clicked_slot()的定义上方,尽管它不是严格需要的,但它提示C++ Qt库clicked_slot应该被调用。(更多decorators的信息参见http://www.pythoncentral.io/python-decorators-overview/)我们之后会了解到@Slot宏更多的信息。现在,只要知道按钮被点击时会发出clicked信号,它会调用它连接的函数,这个函数生动的输出“Ouch!”。
我们接下来看看QPushButton发出它的三个相关信号,pressed,released和clicked。
import sys
from PySide.QtCore import Slot
from PySide.QtGui import * # ... insert the rest of the imports here
# Imports must precede all others ... # Create a Qt app and a window
app = QApplication(sys.argv) win = QWidget()
win.setWindowTitle('Test Window') # Create a button in the window
btn = QPushButton('Test', win) @Slot()
def on_click():
''' Tell when the button is clicked. '''
print('clicked') @Slot()
def on_press():
''' Tell when the button is pressed. '''
print('pressed') @Slot()
def on_release():
''' Tell when the button is released. '''
print('released') # Connect the signals to the slots
btn.clicked.connect(on_click)
btn.pressed.connect(on_press)
btn.released.connect(on_release) # Show the window and run the app
win.show()
app.exec_()
当你点击应用的按钮时,它会输出
pressed
released
clicked
pressed信号是按钮被按下时发出,released信号在按钮释放时发出,最后,所有动作完成后,clicked信号被发出。
完成我们的例子程序
现在,很容易完成上一个教程创建的例子程序了。我们为LayoutExample类添加一个显示问候信息的槽方法。
@Slot()
def show_greeting(self):
self.greeting.setText('%s, %s!' %
(self.salutations[self.salutation.currentIndex()],
self.recipient.text()))
我们使用recipient QLineEdit的text()方法来取回用户输入的文本,salutation QComboBox的currentIndex()方法获得用户的选择。这里同样使用Slot()修饰符来表明show_greeting将被作为槽来使用。然后,我们将按钮的clicked信号与之连接:
self.build_button.clicked.connect(self.show_greeting)
最后,例子像是这样:
import sys
from PySide.QtCore import Slot
from PySide.QtGui import * # Every Qt application must have one and only one QApplication object;
# it receives the command line arguments passed to the script, as they
# can be used to customize the application's appearance and behavior
qt_app = QApplication(sys.argv) class LayoutExample(QWidget):
''' An example of PySide absolute positioning; the main window
inherits from QWidget, a convenient widget for an empty window. ''' def __init__(self):
# Initialize the object as a QWidget and
# set its title and minimum width
QWidget.__init__(self)
self.setWindowTitle('Dynamic Greeter')
self.setMinimumWidth(400) # Create the QVBoxLayout that lays out the whole form
self.layout = QVBoxLayout() # Create the form layout that manages the labeled controls
self.form_layout = QFormLayout() self.salutations = ['Ahoy',
'Good day',
'Hello',
'Heyo',
'Hi',
'Salutations',
'Wassup',
'Yo'] # Create and fill the combo box to choose the salutation
self.salutation = QComboBox(self)
self.salutation.addItems(self.salutations)
# Add it to the form layout with a label
self.form_layout.addRow('&Salutation:', self.salutation) # Create the entry control to specify a
# recipient and set its placeholder text
self.recipient = QLineEdit(self)
self.recipient.setPlaceholderText("e.g. 'world' or 'Matey'") # Add it to the form layout with a label
self.form_layout.addRow('&Recipient:', self.recipient) # Create and add the label to show the greeting text
self.greeting = QLabel('', self)
self.form_layout.addRow('Greeting:', self.greeting) # Add the form layout to the main VBox layout
self.layout.addLayout(self.form_layout) # Add stretch to separate the form layout from the button
self.layout.addStretch(1) # Create a horizontal box layout to hold the button
self.button_box = QHBoxLayout() # Add stretch to push the button to the far right
self.button_box.addStretch(1) # Create the build button with its caption
self.build_button = QPushButton('&Build Greeting', self) # Connect the button's clicked signal to show_greeting
self.build_button.clicked.connect(self.show_greeting) # Add it to the button box
self.button_box.addWidget(self.build_button) # Add the button box to the bottom of the main VBox layout
self.layout.addLayout(self.button_box) # Set the VBox layout as the window's main layout
self.setLayout(self.layout) @Slot()
def show_greeting(self):
''' Show the constructed greeting. '''
self.greeting.setText('%s, %s!' %
(self.salutations[self.salutation.currentIndex()],
self.recipient.text())) def run(self):
# Show the form
self.show()
# Run the qt application
qt_app.exec_() # Create an instance of the application window and run it
app = LayoutExample()
app.run()
运行它你会发现点击按钮可以产生问候信息了。现在我们知道了如何使用我们创建的槽去连接内建的信号,下一个教程中,我们将学习创建并连接自己的信号。
By Ascii0x03
转载请注明出处:http://www.cnblogs.com/ascii0x03/p/5499507.html
PySide——Python图形化界面入门教程(三)的更多相关文章
- PySide——Python图形化界面入门教程(二)
PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...
- PySide——Python图形化界面入门教程(四)
PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your Own Signals and Slots 翻译自:http://pythoncentral ...
- PySide——Python图形化界面入门教程(六)
PySide——Python图形化界面入门教程(六) ——QListView和QStandardItemModel 翻译自:http://pythoncentral.io/pyside-pyqt-tu ...
- PySide——Python图形化界面入门教程(五)
PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...
- PySide——Python图形化界面入门教程(一)
PySide——Python图形化界面入门教程(一) ——基本部件和HelloWorld 翻译自:http://pythoncentral.io/intro-to-pysidepyqt-basic-w ...
- PySide——Python图形化界面
PySide——Python图形化界面 PySide——Python图形化界面入门教程(四) PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your ...
- python+pycharm+PyQt5 图形化界面安装教程
python图形化界面安装教程 配置环境变量 主目录 pip所在目录,及script目录 更新pip(可选) python -m pip install --upgrade pip ps:更新出错一般 ...
- Oracle数据库及图形化界面安装教程详解
百度云盘oracle数据库及图形化界面安装包 链接: https://pan.baidu.com/s/1DHfui-D2n1R6_ND3wDziQw 密码: f934 首先在电脑D盘(或者其他不是C盘 ...
- 如何使用python图形化界面wxPython
GUI库主要有三类:tkinter,wxPython和PyQt5,下面主要是针对wxPython的使用说明. 下面的操作均在win10 + pycharm上进行 wxPython的安装: pip in ...
随机推荐
- poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- echart.gl.js实现动态3D柱状图
echart.gl.js实现动态3D柱状图 一.总结 一句话总结:演示页面的源代码里面一定有所需的所有的js. 二.[js实践篇]——echart.gl.js实现动态3D柱状图 前言 本公司的项目需求 ...
- Android XMPP服务器, BOSH(Http-Binding)和WEB客户端搭建
目标: 搭建一个XMPP服务器, 实现在web page上用javascript与自己XMPP服务器通信, 匿名登录并与任何一个XMPP(Jabber)帐户通信. (Gtalk目前尚有问题) XMPP ...
- JSON 表达式
JSON语法规则: 数据在名称/值对中: 数据由逗号分隔: 大括号保存对象: 中括号保存数组 1.访问对象值: var myObj,x; myObj = {" ...
- js一些编写的函数
第一:它是最常见的 function A(){ } 说明 A(); 第二: var B = function(){ } 方法 B();//这是匿名函数 第三: (function () { ...
- Watchdog机制概述
1. Watchdog初始 Watchdog的中文的“看门狗”,有保护的意思.最早引入Watchdog是在单片机系统中,由于单片机的工作环境容易受到外界磁场的干扰,导致程序“跑飞”,造成整个系统无法正 ...
- BZOJ 2064 - 状压DP
传送门 题目大意: 给两个数组, 数组中的两个元素可以合并成两元素之和,每个元素都可以分裂成相应的大小,问从数组1变化到数组2至少需要多少步? 题目分析: 看到数据范围\(n<=10\), 显然 ...
- 源码分析之Dictionary笔记
接下来我们一步步来熟悉 Dictionary的底层结构实现,下面的MyDictionary等同于源码中的Dictionary看待. 首先我们定义一个类 MyDictionary,类中定义一个结构Ent ...
- unity3D 4.6与上述号码. UI穿透问题,而且不穿透的真机模拟器渗透问题解决
好久没有写博客颓废很长一段时间. . . 不废话. EventSystem.current.IsPointerOverGameObject(); //返回值true 如果是点击UI该.不过貌似没有使用 ...
- ListView与GridView优化
前言 ListView是Android中最常用的控件,通过适配器来进行数据适配然后显示出来,而其性能是个很值得研究的话题.本文与你一起探讨Google I/O提供的优化Adapter方案,欢迎大家交流 ...