IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> tkinter绘制组件(34)——元素等待框 -> 正文阅读

[游戏开发]tkinter绘制组件(34)——元素等待框

引言

好吧,“元素等待框”,又是一个一看就不知所以然的名词……

不巧的是,这个元素控件并没有出现在任何一个常见的GUI框架中,winui本身也没有。那些GUI框架只有通过样式编辑来实现“占位覆盖等待控件”,当然,通过画布也可以实现这个动画提示类控件。不过很明显,常见的GUI框架中确实没有关于这种控件的官方说明。那我来浅说一下。

元素等待框(waitframe)和一些等待提示类控件很像,都是用在耗时任务的时候。不过,元素等待框会覆盖接下来希望展示的控件,比如文本段、文本框、图片等信息展示控件。这样可以避免加载时被误认为卡死的尴尬。

元素等待框是一个占位、动画、提示、可填充、重复使用的控件:

  • 占位,也就是说这个的大小是由编者自行调控的,并且建议完全覆盖加载信息的控件。

  • 动画,也就是等待动画,这样使用者就可以指定程序是不是真的卡死了。

  • 提示,就是等待提示。

  • 可填充,除了动画元素外,这个框架就是一个UI框架,可以写入其它元素控件。

  • 重复使用,可将此作为模板,多次使用。

说了这么多,那就按照这个标准开工吧。其实根本就没有标准。


布局

函数结构

    def add_waitframe(self,pos:tuple,width=300,height=300,fg='#e0e0e0',bg='#ececee'):#元素等待框
    '''
    pos-位置
    width-宽度
    height-高度
    fg-前置标识符颜色
    bg-后置标识符颜色
    '''

等待框架

其实很简单,因为我们需要做的是通过这个元素控件覆盖住我们想要的所有组件,因此必须让这个等待框能够前置。所以,我们直接使用BasicTinUI类来作为框架。

        frame=BasicTinUI(self,width=width,height=height,bg=bg)
        frameid=self.create_window(pos,window=frame,width=width,height=height,anchor='nw')
        uid='waitframe'+str(frameid)
        self.addtag_withtag(uid,frameid)

一般情况下,我们不会一上来就使用元素等待框,或者说是为了更好地重复使用,因此在创建完成后,也就是最后一行,让该控件处于隐藏模式。

        #...
        self.itemconfig(uid,state='hidden')

标识元素

首先讲讲TinUI的waitframe的两个元素。

这两个元素实际上一模一样,只不过颜色不同。两个标识元素更替运动,就可以产生动画。运动路径是从左上角到右下角,每次移动距离为横宽各自的“1/40”。

        itemfg=frame.create_polygon((0,0,width,0,width,height,0,height),outline=fg,fill=fg,width=21)
        itembg=frame.create_polygon((0,0,width,0,width,height,0,height),outline=bg,fill=bg,width=21)
        frame.move(itemfg,-width,-height)
        mx=width/40
        my=height/40
        nowmove=itemfg#当前移动元素
        nowx=0#当前移动元素的横向移动量

移动动画

运动动画逻辑,以itemfg作为先开始元素:

True
False
不足width
大于width
__start函数
判断wait值
判断nowx移动距离
隐藏并退出after
继续调用__start
切换动画标识元素

用代码表示:

        def __start():
            nonlocal nowx,nowmove
            if wait==True:
                if nowx>=width:#移动到位
                    nowx=0
                    #切换移动元素,重新调整层级
                    if nowmove==itemfg:
                        frame.lower(itemfg)
                        frame.move(itembg,-width,-height)
                        nowmove=itembg
                    else:
                        frame.lower(itembg)
                        frame.move(itemfg,-width,-height)
                        nowmove=itemfg
                frame.move(nowmove,mx,my)
                nowx+=mx
                frame.after(25,__start)
            else:
                self.itemconfig(uid,state='hidden')

除此之外,我还为编写者提供了两个方便调用的顶层函数。

        def start():
            nonlocal wait
            wait=True
            self.itemconfig(uid,state='normal')
            __start()
        def end():
            nonlocal wait
            wait=False

完整函数代码

    def add_waitframe(self,pos:tuple,width=300,height=300,fg='#e0e0e0',bg='#ececee'):#元素等待框
        def __start():
            nonlocal nowx,nowmove
            if wait==True:
                if nowx>=width:#移动到位
                    nowx=0
                    #切换移动元素,重新调整层级
                    if nowmove==itemfg:
                        frame.lower(itemfg)
                        frame.move(itembg,-width,-height)
                        nowmove=itembg
                    else:
                        frame.lower(itembg)
                        frame.move(itemfg,-width,-height)
                        nowmove=itemfg
                frame.move(nowmove,mx,my)
                nowx+=mx
                frame.after(25,__start)
            else:
                self.itemconfig(uid,state='hidden')
        def start():
            nonlocal wait
            wait=True
            self.itemconfig(uid,state='normal')
            __start()
        def end():
            nonlocal wait
            wait=False
        frame=BasicTinUI(self,width=width,height=height,bg=bg)
        frameid=self.create_window(pos,window=frame,width=width,height=height,anchor='nw')
        uid='waitframe'+str(frameid)
        self.addtag_withtag(uid,frameid)
        itemfg=frame.create_polygon((0,0,width,0,width,height,0,height),outline=fg,fill=fg,width=21)
        itembg=frame.create_polygon((0,0,width,0,width,height,0,height),outline=bg,fill=bg,width=21)
        frame.move(itemfg,-width,-height)
        mx=width/40
        my=height/40
        nowmove=itemfg#当前移动元素
        nowx=0#当前移动元素的横向移动量
        wait=False
        funcs=FuncList(2)
        funcs.start=start
        funcs.end=end
        self.itemconfig(uid,state='hidden')
        #start()
        return frame,itemfg,itembg,funcs,uid

效果

测试代码

def test11_1(e):
    wffunc.start()
def test11_2(e):
    wffunc.end()
#...
    b.add_button((1220,650),text='获取TinUI相关信息',command=test11_1)
    wf,_,_,wffu...nc,_=b.add_waitframe((1220,700),height=250)
    wf.add_paragraph((150,100),text='Loading . . .',anchor='n')
    wf.add_button2((150,150),text='取消等待?',anchor='n',command=test11_2)
#...

最终效果

在这里插入图片描述


github项目

TinUI的github项目地址

pip下载

pip install tinui

结语

waitframe之所以被称为元素等待框,就是因为它本质上是一个BasicTinUI控件,两个标识符在最底层,编写者可以根据自己的需要在其中添加任何TinUI元素控件。范例里面的按钮和提示文本就是一个简单例子。

🔆tkinter创新🔆

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章           查看所有文章
加:2022-09-25 23:22:59  更:2022-09-25 23:23:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/26 15:53:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码