Python 基础小项目(3)----水果转盘(GUI编程)

本文介绍了一个使用Tkinter实现的大转盘图形用户界面应用,通过按钮代表不同的水果,利用线程控制转盘的旋转和停止,展示了如何在Python中创建交互式的GUI程序。

代码实例:

import tkinter
# 导入线程模块
import threading
import time  # 导入代码的sleep 代码休眠

root = tkinter.Tk()
root.title('大转盘')
root.minsize(300, 300)#转盘尺寸

# 摆放按钮
btn1 = tkinter.Button(root, text='樱桃', bg='red')
btn1.place(x=20, y=20, width=50, height=50)

btn2 = tkinter.Button(root, text='香蕉', bg='white')
btn2.place(x=90, y=20, width=50, height=50)

btn3 = tkinter.Button(root, text='苹果', bg='white')
btn3.place(x=160, y=20, width=50, height=50)

btn4 = tkinter.Button(root, text='西瓜', bg='white')
btn4.place(x=230, y=20, width=50, height=50)

btn5 = tkinter.Button(root, text='鸭梨', bg='white')
btn5.place(x=230, y=90, width=50, height=50)

btn6 = tkinter.Button(root, text='榴莲', bg='white')
btn6.place(x=230, y=160, width=50, height=50)

btn7 = tkinter.Button(root, text='柚子', bg='white')
btn7.place(x=230, y=230, width=50, height=50)

btn8 = tkinter.Button(root, text='葡萄', bg='white')
btn8.place(x=160, y=230, width=50, height=50)

btn9 = tkinter.Button(root, text='草莓', bg='white')
btn9.place(x=90, y=230, width=50, height=50)

btn10 = tkinter.Button(root, text='芒果', bg='white')
btn10.place(x=20, y=230, width=50, height=50)

btn11 = tkinter.Button(root, text='荔枝', bg='white')
btn11.place(x=20, y=160, width=50, height=50)

btn12 = tkinter.Button(root, text='甘蔗', bg='white')
btn12.place(x=20, y=90, width=50, height=50)

# 将所有选项组成列表
fruitlists = [btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12]

# 是否开启循环的标志
isloop = False
# 是否停止标志
stopsign = False  # 是否接收到 stop信号
# 存储停止id------用于进行stop后的重新启动
stopid = None


def round():
    global isloop
    global stopid
    i = 0
    if isinstance(stopid, int):
        i = stopid
    while True:
        # 延时操作
        time.sleep(0.2)
        # 将所有的组件背景变为白色
        for x in fruitlists:
            x['bg'] = 'white'
        # 将当前数值对应的组件变色
        fruitlists[i]['bg'] = 'red'
        # 变量+1
        i += 1
        print('当前i为', i)  # 当前i,用来追踪当前位置
        # 如果i大于最大索引直接归零
        if i >= len(fruitlists):
            i = 0
        if stopsign == True:  # 当停止标志 为真时
            isloop = False
            stopid = i  # 赋值stopid
            break


def stop1():
    global stopsign

    if stopsign == True:  # 当多接收stop1()函数时 ,直接跳过
        return
    stopsign = True


# 建立一个新线程的函数
def newtask():
    global isloop
    global stopsign
    # 建立线程
    stopsign = False
    # print(stopsign)  #打印 点击开始时的stopsign
    t = threading.Thread(target=round)
    # 开启线程运行
    t.start()
    # 设置循环开始标志
    isloop = True


# 开始按钮
btn_start = tkinter.Button(root, text='start', command=newtask)
btn_start.place(x=90, y=125, width=50, height=50)

# 停止按钮
btn_stop = tkinter.Button(root, text='stop', command=stop1)
btn_stop.place(x=160, y=125, width=50, height=50)

root.mainloop()
运行结果

Python实现一些小道具小功能(Python implements some small props) Image-Edit 几个基本的图片编辑工具,包括一下功能: 文件:打开,保存,退出 编辑:放大,缩小,灰度,亮度,旋转,截图 变换:傅里叶变换,离散余弦变换,Radon变换 噪声:高斯,椒盐,斑点,泊松 滤波:高通,低通,平滑,锐化 直方图统计:R直方图,G直方图,B直方图 图像增强:伪彩色,真彩色,直方图均衡,NTSC颜色模型,YCbCr颜色模型,HSV颜色模型 阈值分割 生态学处理 特征提取 图像分类与识别 Beautify-Camera 主要功能 文件:打开,保存,打开摄像头 操作:还原,人脸识别 滤镜:怀旧,木刻,灰色,彩色,风格化,增强细节 调节:亮度,饱和度,伽马变换,边缘保持 磨皮美白:美白度,磨皮程度,磨皮精度 灰度直方图 Calculator 主要功能 基本的加减乘除和开根号等运算 Painting绘画 主要功能 File:新建画板,打开图片,保存图片 Edit:复制,清空画板 Image:翻转 工具:基本画笔,橡皮擦,图形创建工具等 编辑区,色彩调节区,字体调节区等 NotePad 主要功能 基本文本编辑,类似于记事本 RandomPassWord 主要功能 随机生成一串密码,包括大小写字母,数字,符号,可指定长度 Browser 主要功能 基本浏览器功能 MusicPlayer 主要功能 音乐播放器 PyTunes 主要功能 轻量级音乐播放器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值