利用python制作转盘

利用python制作转盘

  1. pip install threading(pip一定要为最新版本,不然容易出错)
  2. pip install tkinter
  3. pip install pyinstaller将turn.py文件进行封装
    文件需要引入的库
import tkinter
import time
import threading
from tkinter import *

通过tkinter创建转盘界面

root=tkinter.Tk()
root.title('"该吃什么?"')
root.minsize(300,300)

btn1=Button(text='奶茶',bg='red')
btn1.place(x=20,y=20,width=50,height=50)

btn2=Button(text='麻辣烫',bg='white')
btn2.place(x=90,y=20,width=50,height=50)

btn3=Button(text='辣子鸡',bg='white')
btn3.place(x=160,y=20,width=50,height=50)

btn4=Button(text='黄焖鸡',bg='white')
btn4.place(x=230,y=20,width=50,height=50)

btn5=Button(text='肠粉',bg='white')
btn5.place(x=230,y=90,width=50,height=50)

btn6=Button(text='圣代',bg='white')
btn6.place(x=230,y=160,width=50,height=50)

btn7=Button(text='鸡排',bg='white')
btn7.place(x=230,y=230,width=50,height=50)

btn8=Button(text='泡面',bg='white')
btn8.place(x=160,y=230,width=50,height=50)

btn9=Button(text='沙县',bg='white')
btn9.place(x=90,y=230,width=50,height=50)

btn10=Button(text='烧腊',bg='white')
btn10.place(x=20,y=230,width=50,height=50)

btn11=Button(text='水煮鱼片',bg='white')
btn11.place(x=20,y=160,width=50,height=50)

btn12=Button(text='螺蛳粉',bg='white')
btn12.place(x=20,y=90,width=50,height=50)

btn_start=Button(root,text='开始',command=newtask)
btn_start.place(x=90,y=120,width=50,height=50)

btn_stop=Button(root,text='停止',command=stop)
btn_stop.place(x=160,y=120,width=50,height=50)

carlist=[btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12]

效果图如下:
在这里插入图片描述
循环判断,转盘开始转动

#判断是否循环的标志
#isloop=False

def round():
    #判断是否开始循环
    #if isloop==True:
        #return
    #初始化计数变量
    i=0
    #死循环
    while True:
        time.sleep(0.1)
        #将所有组件背景变为白色
        for x in carlist:
            x['bg']='white'
        #将当前数值对应的组件变色
        carlist[i]['bg']='red'
        i+=1
        #如果i大于最大索引直接归零
        if i>=len(carlist):
            i=0
        if functions==True:
            continue;
        else:
            break

建立一个进程

def newtask():
    #global isloop
    global functions
    #建立新线程
    t=threading.Thread(target=round)
    t.start()
    #设置程序开始标志
    functions=True

停止按钮

#"停止"按钮事件:停止循环
def stop():
    #global isloop
    global functions
    functions=False
    #isloop=False
#开始/停止按钮

按钮可以根据自己需要更改,由于电脑录不了屏,就不发图了
完整代码
turn.py文件

import tkinter
import time
import threading
from tkinter import *

root=tkinter.Tk()
root.title('"该吃什么?"')
root.minsize(300,300)

btn1=Button(text='奶茶',bg='red')
btn1.place(x=20,y=20,width=50,height=50)

btn2=Button(text='麻辣烫',bg='white')
btn2.place(x=90,y=20,width=50,height=50)

btn3=Button(text='辣子鸡',bg='white')
btn3.place(x=160,y=20,width=50,height=50)

btn4=Button(text='黄焖鸡',bg='white')
btn4.place(x=230,y=20,width=50,height=50)

btn5=Button(text='肠粉',bg='white')
btn5.place(x=230,y=90,width=50,height=50)

btn6=Button(text='圣代',bg='white')
btn6.place(x=230,y=160,width=50,height=50)

btn7=Button(text='鸡排',bg='white')
btn7.place(x=230,y=230,width=50,height=50)

btn8=Button(text='泡面',bg='white')
btn8.place(x=160,y=230,width=50,height=50)

btn9=Button(text='沙县',bg='white')
btn9.place(x=90,y=230,width=50,height=50)

btn10=Button(text='烧腊',bg='white')
btn10.place(x=20,y=230,width=50,height=50)

btn11=Button(text='水煮鱼片',bg='white')
btn11.place(x=20,y=160,width=50,height=50)

btn12=Button(text='螺蛳粉',bg='white')
btn12.place(x=20,y=90,width=50,height=50)

carlist=[btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12]
#判断是否循环的标志
#isloop=False

def round():
    #判断是否开始循环
    #if isloop==True:
        #return
    #初始化计数变量
    i=0
    #死循环
    while True:
        time.sleep(0.1)
        #将所有组件背景变为白色
        for x in carlist:
            x['bg']='white'
        #将当前数值对应的组件变色
        carlist[i]['bg']='red'
        i+=1
        #如果i大于最大索引直接归零
        if i>=len(carlist):
            i=0
        if functions==True:
            continue;
        else:
            break
#'开始按钮事件':建立一个新进程的函数
def newtask():
    #global isloop
    global functions
    #建立新线程
    t=threading.Thread(target=round)
    t.start()
    #设置程序开始标志
    functions=True
#"停止"按钮事件:停止循环
def stop():
    #global isloop
    global functions
    functions=False
    #isloop=False
#开始/停止按钮
btn_start=Button(root,text='开始',command=newtask)
btn_start.place(x=90,y=120,width=50,height=50)

btn_stop=Button(root,text='停止',command=stop)
btn_stop.place(x=160,y=120,width=50,height=50)

root.mainloop()

到此,一个转盘就弄好了。
但为了饭点方便使用,接下来将turn.py文件通过pystaller封装为exe文件,电脑双击即可打开
步骤:

  1. window+r,enter进入cmd界面,cd切换到python/script(具体看你存在哪一个磁盘)
  2. 输入pip install pyinstaller会自动安装
  3. 安装完模块,输入Pyinstaller -F 文件名.py
  4. 打包完成!
    最好新建一个目录,将turn.py文件放在该目录下,cmd界面切换好路径
    在这里插入图片描述
    在这里插入图片描述
    出现successfully,表示打包成功!
    在所创建目录下会生成一个子目录dist,里面放着封装好的turn.exe文件,双击打开即可,也可以直接发送给好友,前提是要电脑打开。
    在这里插入图片描述
    在这里插入图片描述

本文涉及代码引用其它书籍,如侵,请联删!

  • 7
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值