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 ...
随机推荐
- 【t062】最厉害的机器人
Time Limit: 1 second Memory Limit: 128 MB [问题描述] [背景] Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ [问题描述] ...
- apt-get install安装软件时出现依赖错误解决方式
在使用apt-get install安装软件时,常常会遇到如上图所看到的错误.该错误的意思为缺少依赖软件.解决方式为: aptitude install golang-go
- [React] Recompose: Override Styles & Elements Types in React
When we move from CSS to defining styles inside components we lose the ability to override styles wi ...
- NOIP模拟 回文序列 - DP
题意: 如果一个字符串等于s和t的长度之和(\(l \le 50\)),并且可以拆成两个字符串子序列,分别与s和t相同,那么它就是s和t的一个并字符串(从字符串中选出若干个可以不连续的字符按照原序列写 ...
- .net core ——微服务内通信Thrift和Http客户端响应比较
原文:.net core --微服务内通信Thrift和Http客户端响应比较 目录 1.Benchmark介绍 2.测试下微服务访问效率 3.结果 引用链接 1.Benchmark介绍 wiki中有 ...
- MySQL中关于OR条件的优化
转载 MySQL在 5.0版本中引入新特性:索引合并优化(Index merge optimization),当查询中单张表可以使用多个索引时,同时扫描多个索引并将扫描结果进行合并. 该特新主要应用于 ...
- iOS 直播
待解决: 貌似苹果规定10M以内的视频可以用RTMP,以上的必须用HLS ? IOS非直播超过10分钟只能用hls,ios上有规定? 待尝试: 用Vitamion内核开发的可以自定义界面的视频播放器 ...
- 【t008】钱币变换问题
Time Limit: 2 second Memory Limit: 32 MB [问题描述] 给定 2*n 个方格,将其排成一行.选择两个相邻的方格,设置为空方格,初始时不放钱币.而其余的方格共放入 ...
- jsp页面遍历List<Map<String,Object>>
多表联查会有此类结果出现, 查阅发现基本解决思路是双重遍历,获取map,entry.value等方法. 最终发现可以使用c:forEach单次遍历,map中的key值大写,即可得到object. Co ...
- java取奇偶数的基本练习
public class JiOu { public static void main(String[] args) { int a = 11; System.out.println("这个 ...