PyQt5 textbox

Thu 12 October 2017 by Little Captain

textbox

The widget is called QLineEdit and has the methods setText() to set the textbox value and text() to get the value.

We can set the size of the textbox using the resize(width,height) method. The position can be set using the move(x,y) method or using a …

read more

PyQt5 messagebox

Thu 12 October 2017 by Little Captain

messagebox

To show a messagebox we need to import QMessageBox. We use the method QMessageBox.question() to display the messagebox

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title …
read more

PyQt5 signals and slots

Thu 12 October 2017 by Little Captain

signals and slots

Graphical applications (GUI) are event-driven, unlike console or terminal applications. A users action like clicks a button or selecting an item in a list is called an event.

If an event takes place, each PyQt5 widget can emit a signal. A signal does not execute any action …

read more

PyQt5 buttons

Thu 12 October 2017 by Little Captain

buttons

PyQt5 supports buttons using the QPushButton class. This class is inside the PyQt5.QtWidgets group. The button can be created by calling the constructor QPushButton with the text to display as parameter

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore …
read more

PyQt5 statusbar

Thu 12 October 2017 by Little Captain

statusbar

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtGui import QIcon

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 status bar example - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle …
read more

PyQt5 window

Thu 12 October 2017 by Little Captain

window

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 simple window - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title …
read more

PyQt(Chapter 07)

Wed 11 October 2017 by Little Captain

PyQt

窗口和对话框

  • 对话框包括两类: modal、modeless
  • modal: 禁止用户与程序的其他部分交互
  • modeless: 与 modal 相对, 用户可以自由的与应用程序的其他部分交互

创建应用程序

  • 通过简单的纯文本编辑器创建
  • 使用 Qt Designer

使用 Code 创建 GUI 程序

  • 这是针对 Qt4 的代码, Qt5 运行要不起来
# 导入需要的模块
import sys
from PyQt5 import QtGui, QtCore
# QWidget 是所有的交互对象的基类
class demowind(QtGui.QWidget):
    # 构造函数
    def __init__(self, parent = None):
        QtGui.QWidget.__init__ …
read more

PyQt(Chapter 06)

Tue 10 October 2017 by Little Captain

File Handling

  • 文件是数据对象序列的容器, 被描述为字节序列
  • 文件处理是读写文件的技术

  • 文件分类

  • 文本文件: 容易理解, 但是不易更新
  • 二进制文件: 优化了处理速度. 通过 offset 写入数据, 通过 seek() 方法访问特定的数据
  • Pickled files: 用于存储对象. 为了优化性能, 将对象存储为二进制格式

  • 处理文件三步走:

  • 打开文件
  • 执行文件操作(读、写、更新)
  • 关闭文件

打开文件

# file_name 文件名
# mode 打开文件的目的
open(file_name, mode) # 返回 file handler 对象
f = open('xyz.txt', 'w')
  • mode
r  # 打开 for …
read more

PyQt(Chapter 05)

Mon 09 October 2017 by Little Captain

Classes

  • 类是数据和操作的模板
  • 通过类可以创建实例(对象)
# 多个父类用逗号隔开
class classname[(base-classes)]:
    statement(s)

类对象的属性

  • 类属性, 不需要实例对象, 使用类名引用
class rect:
    l = 8
print(rect.l)
class rect(object):
    pass
rect.l = 10
print(rect.l)

内建的类属性

  • 一个类隐式的设置了一些类属性, 我们称之为内建类属性
__name__ # 类名
__bases__ # 一个 tuple 指示 base classes
__dict__ # 通过 key/value 存储类的自定义属性. rect …
read more

PyQt(Chapter 04)

Sat 07 October 2017 by Little Captain

函数

  • 在 Python 中, 函数别当做对象

函数的定义

def function-name(parameters):
    statements(s)

返回语句

  • 返回语句是可选的, 如果没有, 函数返回 None

默认参数值

def sum(x, y = 10):
    return x + y
print(sum(10))
print(sum(5, 8))

关键字参数

def volume(l, b=5, h=10):
    print('l is', l, 'and b is …
read more