PySide——Python图形化界面入门教程(三)

——使用内建新号和槽

——Using Built-In Signals and Slots

上一个教程中,我们学习了如何创建和建立交互widgets,以及将他们布局的两种不同的方法。今天我们继续讨论Python/Qt应用响应用户触发的事件:信号和槽。

当用户执行一个动作——点击按钮,选择组合框的值,在文本框中打字——这个widget就会发出一个信号。这个信号自己什么都不做,它必须和槽连接起来才行。槽是一个接受信号的执行动作的对象。

连接内建PySide/PyQt信号

Qt widgets有许多的内建信号。例如,当QPushButton被点击的时候,它发出它的clicked信号。clicked信号可以被连接到一个拥有槽功能的函数(只是一个概要,需要更多内容去运行)

1 @Slot()2 defclicked_slot():3 '''This is called when the button is clicked.'''

4 print('Ouch!')5

6

7 #Create the button

8 btn = QPushButton('Sample')9

10 #Connect its clicked signal to our slot

11 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。

1 importsys2 from PySide.QtCore importSlot3 from PySide.QtGui import *

4

5 #... insert the rest of the imports here

6 #Imports must precede all others ...

7

8 #Create a Qt app and a window

9 app =QApplication(sys.argv)10

11 win =QWidget()12 win.setWindowTitle('Test Window')13

14 #Create a button in the window

15 btn = QPushButton('Test', win)16

17 @Slot()18 defon_click():19 '''Tell when the button is clicked.'''

20 print('clicked')21

22 @Slot()23 defon_press():24 '''Tell when the button is pressed.'''

25 print('pressed')26

27 @Slot()28 defon_release():29 '''Tell when the button is released.'''

30 print('released')31

32 #Connect the signals to the slots

33 btn.clicked.connect(on_click)34 btn.pressed.connect(on_press)35 btn.released.connect(on_release)36

37 #Show the window and run the app

38 win.show()39 app.exec_()

当你点击应用的按钮时,它会输出

pressed

released

clicked

pressed信号是按钮被按下时发出,released信号在按钮释放时发出,最后,所有动作完成后,clicked信号被发出。

完成我们的例子程序

现在,很容易完成上一个教程创建的例子程序了。我们为LayoutExample类添加一个显示问候信息的槽方法。

@Slot()defshow_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)

最后,例子像是这样:

1 importsys2 from PySide.QtCore importSlot3 from PySide.QtGui import *

4

5 #Every Qt application must have one and only one QApplication object;

6 #it receives the command line arguments passed to the script, as they

7 #can be used to customize the application's appearance and behavior

8 qt_app =QApplication(sys.argv)9

10 classLayoutExample(QWidget):11 '''An example of PySide absolute positioning; the main window12 inherits from QWidget, a convenient widget for an empty window.'''

13

14 def __init__(self):15 #Initialize the object as a QWidget and

16 #set its title and minimum width

17 QWidget.__init__(self)18 self.setWindowTitle('Dynamic Greeter')19 self.setMinimumWidth(400)20

21 #Create the QVBoxLayout that lays out the whole form

22 self.layout =QVBoxLayout()23

24 #Create the form layout that manages the labeled controls

25 self.form_layout =QFormLayout()26

27 self.salutations = ['Ahoy',28 'Good day',29 'Hello',30 'Heyo',31 'Hi',32 'Salutations',33 'Wassup',34 'Yo']35

36 #Create and fill the combo box to choose the salutation

37 self.salutation =QComboBox(self)38 self.salutation.addItems(self.salutations)39 #Add it to the form layout with a label

40 self.form_layout.addRow('&Salutation:', self.salutation)41

42 #Create the entry control to specify a

43 #recipient and set its placeholder text

44 self.recipient =QLineEdit(self)45 self.recipient.setPlaceholderText("e.g. 'world' or 'Matey'")46

47 #Add it to the form layout with a label

48 self.form_layout.addRow('&Recipient:', self.recipient)49

50 #Create and add the label to show the greeting text

51 self.greeting = QLabel('', self)52 self.form_layout.addRow('Greeting:', self.greeting)53

54 #Add the form layout to the main VBox layout

55 self.layout.addLayout(self.form_layout)56

57 #Add stretch to separate the form layout from the button

58 self.layout.addStretch(1)59

60 #Create a horizontal box layout to hold the button

61 self.button_box =QHBoxLayout()62

63 #Add stretch to push the button to the far right

64 self.button_box.addStretch(1)65

66 #Create the build button with its caption

67 self.build_button = QPushButton('&Build Greeting', self)68

69 #Connect the button's clicked signal to show_greeting

70 self.build_button.clicked.connect(self.show_greeting)71

72 #Add it to the button box

73 self.button_box.addWidget(self.build_button)74

75 #Add the button box to the bottom of the main VBox layout

76 self.layout.addLayout(self.button_box)77

78 #Set the VBox layout as the window's main layout

79 self.setLayout(self.layout)80

81 @Slot()82 defshow_greeting(self):83 '''Show the constructed greeting.'''

84 self.greeting.setText('%s, %s!' %

85 (self.salutations[self.salutation.currentIndex()],86 self.recipient.text()))87

88 defrun(self):89 #Show the form

90 self.show()91 #Run the qt application

92 qt_app.exec_()93

94 #Create an instance of the application window and run it

95 app =LayoutExample()96 app.run()

View Code

运行它你会发现点击按钮可以产生问候信息了。现在我们知道了如何使用我们创建的槽去连接内建的信号,下一个教程中,我们将学习创建并连接自己的信号。

By Ascii0x03

转载请注明出处:http://www.cnblogs.com/ascii0x03/p/5499507.html

qpython3可视图形界面_PySide——Python图形化界面入门教程(三)相关推荐

  1. python图形用户界面pyside_PySide——Python图形化界面入门教程(一)

    标签: PySide--Python图形化界面入门教程(一) --基本部件和HelloWorld 原文链接:http://pythoncentral.io/intro-to-pysidepyqt-ba ...

  2. python界面设计-python图形化界面设计tkinter

    匿名用户 1级 2017-12-13 回答 python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: Tkinter模块("Tk 接口")是P ...

  3. 现今主流计算机语言,现今主流的Python图形化界面主要有哪些

    现今主流的Python图形化界面主要有哪些 发布时间:2020-10-23 20:08:59 来源:亿速云 阅读:114 作者:小新 这篇文章将为大家详细讲解有关现今主流的Python图形化界面主要有 ...

  4. python图形化界面开发工具,python如何做图形化界面

    Python tkinter能做出好看的图形界面么 谷歌人工智能写作项目:小发猫 python 图形化界面 使用wxpython,import wxapp = ()win = wx.Frame(Non ...

  5. python图形化界面

    python图形化界面 一.定义理解 Python自带了tkinter 模块,实质上是一种流行的面向对象的GUI工具包 TK 的Python编程接口,提供了快速便利地创建GUI应用程序的方法.其图像化 ...

  6. python图形化界面教程_python图形化界面开发教程

    python图形化界面开发教程内容摘要 python图形化界面开发教程白萝卜:泰兴电工教程,白了点,白兰地是在红葡萄酒的基础.基金从业资格教程学校,白开水.苜蓿干草.提摩西干草.兔粮方法:白居易< ...

  7. python中的图形界面设计_python图形化界面设计(tkinter)一全面介绍

    3.3.单选按钮:(Radiobutton)是为了响应故乡排斥的若干单选项的单击事件以触发运行自定义函数所设的,该控件排除具有共有属性外,还具有显示文本(text).返回变量(variable).返回 ...

  8. python图形界面设计代码_(八)Python 图形化界面设计

    3.1.文本输入和输出相关控件:文本的输入与输出控件通常包括:标签(Label).消息(Message).输入框(Entry).文本框(Text).他们除了前述共同属性外,都具有一些特征属性和功能. ...

  9. Python 图形化界面设计

    1.图形化界面设计的基本理解 当前流行的计算机桌面应用程序大多数为图形化用户界面(Graphic User Interface,GUI),即通过鼠标对菜单.按钮等图形化元素触发指令,并从标签.对话框等 ...

最新文章

  1. 商汤被曝已获准在香港上市,计划筹资逾10亿美元
  2. GNU make manual 翻译( 一百五十四)
  3. android子线程没有运行完,android假如主线程依赖子线程A的执行结果,如何让A执行完成,之后主线程再往下执行呢?...
  4. java 一些常用的代码(转载)
  5. 小米手机怎么获取 ROOT 权限
  6. “VT-x is disabled in BIOS”的解决办法【Android Studio】【操作环境:win 7 台式机】【查看Android Studio版本】
  7. logback读取src/test/resource下的配置文件
  8. mysql6.0_MySQL6.0安装
  9. dcdc升压计算器excel_DC-DC升降压芯片(MC34063A/33063)典型电路与元件参数在线计算_三贝计算网_23bei.com...
  10. C#创建文件,覆盖文件,读取文件
  11. 用python做youtube自动化下载器 代码
  12. 录音转成文字的方法分享
  13. 用python画画简单代码_Python3使用PyQt5制作简单的画板/手写板实例
  14. 这家200多年历史的中华老字号,是如何赢得今年快手中秋月饼品牌冠军的?
  15. Unreal Engine 4 UE4 CAVE VR 立体 Stereo nDisplay 多通道
  16. 内部专家亲自揭秘!滴滴对象存储系统的演进之路
  17. 一只大二狗的Android历程--文件输入输出流 SharedPreference
  18. 火车票是一门什么生意 「上篇」
  19. 2019CCPC网络赛部分题解
  20. 银河英雄传说 acwing-238 并查集

热门文章

  1. 发票抬头是什么意思?
  2. eureka自我保护时间_SpringCloud Eureka自我保护机制
  3. docker修改容器映射的端口_解密 Docker 挂载文件,宿主机修改后容器里文件没有修改...
  4. 渗透测试小马(一句话)篇
  5. php 七牛云fetch,七牛云调用类
  6. 微信小程序通过getUserProfile和wx.login获取后端的token
  7. 网众linux安装教程,网众Linux搭建Samba教程
  8. oracle一句话倒过来,oracle一些基本语句
  9. java web modules_使用Java web工程建立Maven Web Module工程
  10. ssh汉字乱码怎么办_[转]SSH Secure Shell Client中文乱码的解决办法