5 - 基础控件

|- 5.1-文字 draw_text

语法:控件对象名 = GUI对象.draw_text(x, y, w, text, color, origin, onclick, font_family,font_size,angle)

import time
from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类
def info_text_on_click():
    print("文字被点击")

info_text = gui.draw_text(x=120, y=320, text='你好',origin='bottom' ,onclick=info_text_on_click)

info_text2 = gui.draw_text(x=0, y=100, text='1234567890')
info_text3 = gui.draw_text(x=0, y=150, w=50, text='1234567890')#换行演示

# 加载自己下载的字库文件,输入参数为本地字库文件所在路径
# 字库下载参考网站:https://www.fontspace.com/category/led
#font_family = gui.load_font('Segment7-4Gml.otf') 
#gui.draw_text(x=20, y=80, text='13:11', font_size=40, font_family=font_family) #使用字库显示

while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)

unihiker库的画图控件均使用对象方式更新,因此需要给每个控件定一个对象名字,后续使用对象名调整功能。

|- 5.2-数码管字体 draw_digit

语法:控件对象名 = GUI对象.draw_digit(x, y, text, color, origin, onclick, font_size)

from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类

gui.draw_digit(x=120, y=160, text='2022-02-01', origin = "center",color="red",font_size=25)#数码管字体显示

import time
while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)

自定义字体的方法:[教程]如何使用第三方字库显示点阵风格文字?

|- 5.3-图片 draw_image

语法:控件对象名 = GUI对象.draw_image(x, y, w, h, image,origin,onclick)

#  -*- coding: UTF-8 -*-
from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类

# 传入图片文件路径的方式
## 直接传入图片,原始尺寸
img_image2 = gui.draw_image(x=0, y=0, image='logo.png')
## 修改尺寸
img_image = gui.draw_image(x=120, y=100, w=80, h=50,  image='logo.png', origin='center', onclick=lambda: print("image clicked"))

# 使用PIL库传入图片对象的方式
from PIL import Image
img_image3 = gui.draw_image(x=10, y=180, image= Image.open('logo.png'))

import time
while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)

|- 5.3-表情 draw_emoji

语法:控件对象名 = GUI对象.draw_emoji(x, y, w, h, emoji,duration,origin,onclick)

from unihiker import GUI   #导入包
import time
gui=GUI()  #实例化GUI类

emj1 = gui.draw_emoji(x=0, y=0, w=100, h=100, emoji="Wink", duration=0.1, onclick=lambda:cb("emojis clicked"))
emj2 = gui.draw_emoji(x=120, y=200, w=100, h=100, emoji="Smile", duration=1,origin="center" ,
onclick=lambda:cb("emojis clicked"))
# 传入图片文件路径的方式,同级目录下放多帧图片文件,custom-1.png、custom-2.png、custom-3.png...
# gui.draw_emoji(x=0, y=0, w=100,  emoji="custom", duration=0.2)

while True:
    time.sleep(1) #等待防止程序退出看不到效果

|- 5.4-按钮 add_button

注:按钮控件为特殊控件,一直显示在最顶层。
注:unihiker库中图片及填充矩形也具有onclick事件,因此如需更好看的效果可以使用其他控件实现。
注:按钮的onclick默认传入的是函数名字,不带括号

语法:控件对象名 = GUI对象.add_button(x, y, w, h, text, origin, state,onclick)

from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类
gui.add_button(x=120, y=110, w=100, h=30, text="按钮", origin='center', onclick=lambda: print("button clicked"))
gui.add_button(x=120, y=210, w=100, h=30, text="按钮", origin='center', onclick=lambda: print("button clicked"),state="disabled")

import time
while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)

#多个按钮调用同一个函数并传参示例
from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类

def btclick(data):
    print(data)
    txt.config(text=str(data))

txt=gui.draw_text(text="0",x=120,y=10,font_size=20,origin="center",color="#0000FF")

gui.add_button(x=120, y=100, w=100, h=30, text="按钮1", origin='center', onclick=lambda: btclick(1))
gui.add_button(x=120, y=150, w=100, h=30, text="按钮2", origin='center', onclick=lambda: btclick(2))
gui.add_button(x=120, y=200, w=100, h=30, text="按钮3", origin='center', onclick=lambda: btclick(3))

import time
while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)

|- 5.5-时钟 draw_clock

语法:控件对象名 = GUI对象.draw_clock(x, y, r, h, m, s, color, onclick)

from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类
gui.draw_clock(x=120, y=160, r=80, h=10, m=8, s=0, color="#f8ad30", onclick=lambda: print("clock clicked"))

import time
while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)

|- 5.6-填充时钟 fill_clock

语法:控件对象名 = GUI对象.fill_clock(x, y, r, h, m, s, color,fill,style, onclick)

from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类
gui.fill_clock(x=120, y=100, r=60, h=3, m=4, s=5, color=(255, 255, 255), fill="#57b5ff", onclick=lambda: print("clock1 clicked"))
gui.fill_clock(x=120, y=230, r=60, h=6, m=7, s=8, style='dark', onclick=lambda: print("clock2 clicked"))

import time
while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)

|- 5.7-二维码 draw_qr_code

语法:控件对象名 = GUI对象.draw_qr_code(x, y, w, text, origin, onclick)

from unihiker import GUI   #导入包
gui=GUI()  #实例化GUI类
gui.draw_qr_code(x=100, y=200, w=100, text="https://unihiker.com", origin ="center",onclick=lambda:print("qr clicked"))

import time
while True:
    #增加等待,防止程序退出和卡住
    time.sleep(1)