美文网首页程序员
官方提供的基础指南二

官方提供的基础指南二

作者: cloveses | 来源:发表于2018-11-02 15:16 被阅读0次

4.按钮使用

import sys
from PySide2.QtWidgets import QApplication, QPushButton

def say_hello(): 
  print("Button clicked, Hello!")

# Create the Qt Application
app = QApplication(sys.argv)

# Create a button, connect it and show it
button = QPushButton("Click me")
button.clicked.connect(say_hello)
button.show()
# Run the main Qt loop
app.exec_()

程序中先定义了say_hello()函数,用于在按钮被点击时调用。之后,初始化了一个QPushButton类,并通过类的QPushButton的方法将点击时要执行的动作函数与此绑定。
每点击一次,在控制台输出一串字符,如下图所示:


Snipaste_2018-11-02_14-36-59.png

如果希望点击按钮时退出程序,可以调用QApplication实例的exit函数,即通过以下语句实现:

button.clicked.connect(app.exit)

5.对话框使用

程序的基本框架

代码如下:

import sys
from PySide2.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setWindowTitle("My Form")

if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)
    # Create and show the form
    form = Form()
    form.show()
    # Run the main Qt loop
    sys.exit(app.exec_())

与之前的程序有所不同:程序中自定义了一个Form类,继承了QDialog类。并在类中调用了父类的初始化方法和从父类继承而来的setWindowTitle()方法,设置了窗口的标题。
其余部分与之前实例结构相同。

添加其他图形组件

添加图形组件有四个基本步骤:
1.创建图形组件

self.edit = QLineEdit('Write my name here...')
self.button = QPushButton("Show Greetings")

以上创建了两个图形组件
2.创建布局管理器,用于对组件进行添加和布局管理

layout = QVBoxLayout()

3.将组件加入布局管理器

layout.addWidget(self.edit)
layout.addWidget(self.button)

4.将布局管理添加到父级组件上

self.setLayout(layout)

以上步骤都需要在类的初始化时完成,所以应将以上代码全部放入Form类的init()方法的最后。

定义按钮的动作函数并绑定至按钮

def greetings(self):
    print("Hello {}".format(self.edit.text()))

然后使用button的属性绑定greeting方法:

self.button.clicked.connect(self.greetings)

完整的代码如下:

import sys
from PySide2.QtWidgets import (QLineEdit, QPushButton, QApplication,
    QVBoxLayout, QDialog)

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        # Create widgets
        self.edit = QLineEdit("Write my name here")
        self.button = QPushButton("Show Greetings")
        # Create layout and add widgets
        layout = QVBoxLayout()
        layout.addWidget(self.edit)
        layout.addWidget(self.button)
        # Set dialog layout
        self.setLayout(layout)
        # Add button signal to greetings slot
        self.button.clicked.connect(self.greetings)
    # Greets the user
    def greetings(self):
        print ("Hello %s" % self.edit.text())

if __name__ == '__main__':

    # Create the Qt Application
    app = QApplication(sys.argv)
    # Create and show the form
    form = Form()
    form.show()
    # Run the main Qt loop
    sys.exit(app.exec_())

运行效果如下图:


Snipaste_2018-11-02_15-13-15.png

相关文章

  • 官方提供的基础指南二

    4.按钮使用 程序中先定义了say_hello()函数,用于在按钮被点击时调用。之后,初始化了一个QPushBut...

  • 官方提供的基础指南一

    1.Hello World 程序 2.QML 实现GUI界面 QML 使用声明式语法实现GUI界面,要使用QML实...

  • 官方提供的基础指南三-信号和槽

    信号和槽机制为对组件的事件进行绑定提供了灵活的的支持,简化了GUI程序开发的方式。基本使用方式在上篇文章中已经涉及...

  • 极光推送(基础配置)

    官方操作指南Android SDK 集成指南参考:Android之极光推送SDK集成和基础功能的实现 目录 注册账...

  • Spring开发指南,带你进入Spring的世界

    Spring开发指南是Spring官方提供的Spring技术的入门级学习指南,内容主题全面覆盖了Spring技术栈...

  • 目录

    Spark之参数介绍 Spark之性能优化2.1. 官方性能优化指南2.2. Spark性能优化指南——基础篇2....

  • 上线AppStore被拒原因

    苹果官方提供的 App Store 审核指南(中文) 被拒绝上架记录 一、UGC Guideline 1.2 -...

  • Elasticsearch用户指南 三 API约定

    版本5.0 官方文档英文版 相关文章: Elasticsearch用户指南 一 基础(1) Elasticsear...

  • 微信小程序 文档目录

    一、官方文档 官方文档 二、设计规范 设计指南一 设计指南二-视觉规范 拾旧微信小程序设计规范 小程序减肥过程 三...

  • Introduction to CFNetwork Progra

    CFNetwork编程指南介绍 @官方文档翻译-李冰 @译文 CFNetwork提供了网络协议抽象库的最为核心的服...

网友评论

    本文标题:官方提供的基础指南二

    本文链接:https://www.haomeiwen.com/subject/dgfcxqtx.html