Skip to content

TNumberBoxDemo 项目分析

项目信息

文件结构

TNumberBoxDemo/
├── Extractor_Icon.ico
├── Project1.py
├── Project1.xml
├── Unit1.py
├── Unit1.pydfm
├── Unit1.sct
├── Unit1.sfm

主程序文件: Project1.py

from glcl import *
from Unit1 import *

def main():
    Application.Initialize()
    Application.Title = 'Project1'
    Application.MainFormOnTaskbar = True
    Application.Icon.LoadFromFile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Extractor_Icon.ico"))
    MainForm = Form1(Application)
    MainForm.Show()
    FreeConsole()
    Application.Run()
    MainForm.Free()

if __name__ == '__main__':
    main()

单元文件: Unit1.py

import os
from glcl import *

class Form1(Form):

    def __init__(self, owner):
        self.Button1 = Button(self)
        self.NumberBox1 = NumberBox(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.NumberBox1.OnChangeValue = self.NumberBox1ChangeValue
        self.Button1.OnClick = self.Button1Click

        # 创建一个 TNumberBox 实例
        self.number_box = NumberBox(self)
        self.number_box.Parent = self   # 设置父控件为窗体
        self.number_box.SetBounds(50, 50, 200, 30)  # 设置位置和大小
        self.number_box.MinValue = 0  # 设置最小值
        self.number_box.MaxValue = 100  # 设置最大值
        self.number_box.Value = 50  # 设置默认值

        # 创建一个按钮,用于显示输入的数字
        self.button = Button(self)
        self.button.Parent = self   # 设置父控件为窗体
        self.button.Caption = 'Show Value'  # 设置按钮标题
        self.button.SetBounds(50, 100, 200, 30)  # 设置按钮位置和大小
        self.button.OnClick = self.show_value  # 绑定按钮点击事件

    def show_value(self, Sender):
        """显示当前输入的数字"""
        value = self.number_box.Value  # 获取当前输入的值
        Application.MessageBox(f'Current Value: {value}', 'Info', MB_OK)  # 弹出消息框显示值

    def Button1Click(self, Sender):
        self.Caption = str(self.NumberBox1.Value)

    def NumberBox1ChangeValue(self, Sender):
        self.Caption = str(self.NumberBox1.Value)

设计文件: Unit1.sct

def Button1Click(Sender): 
def NumberBox1ChangeValue(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 166
  ClientWidth = 262
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object NumberBox1: TNumberBox
    Left = 24
    Top = 120
    Width = 121
    Height = 28
    Mode = nbmCurrency
    TabOrder = 0
    SpinButtonOptions.Placement = nbspInline
    SpinButtonOptions.ArrowColor = clMaroon
    OnChangeValue = NumberBox1ChangeValue
  end
  object Button1: TButton
    Left = 160
    Top = 120
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • NumberBox1 (TNumberBox)
  • Button1 (TButton)

技术特点

  • 包含3个GUI组件
  • 定义2个事件处理

窗体属性

  • caption: Button1
  • height: 166
  • width: 262

代码分析

导入的模块: - import os - from glcl import *

定义的类: - Form1

定义的方法: - init - show_value - Button1Click - NumberBox1ChangeValue

事件绑定: - self.Button1.OnClick = self.Button1Click - self.button.OnClick = self.show_value # 绑定按钮点击事件