python实现大转盘抽奖效果

yipeiwu_com5年前Python基础

本文实例为大家分享了python实现大转盘抽奖的具体代码,供大家参考,具体内容如下

选择转盘中的某一个方框,来进行抽奖

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
 #判断是否开始循环
 if isloop == True:
  return
 i=1
 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()

效果图:

就是上图这个界面了:

start 开始按钮

stop 结束按钮

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 实现数据库更新脚本的生成方法

我在工作的时候,在测试环境下使用的数据库跟生产环境的数据库不一致,当我们的测试环境下的数据库完成测试准备更新到生产环境上的数据库时候,需要准备更新脚本,真是一不小心没记下来就会忘了改了哪...

Python中的单下划线和双下划线使用场景详解

Python中的单下划线和双下划线使用场景详解

单下划线 单下划线用作变量 最常见的一种使用场景是作为变量占位符,使用场景明显可以减少代码中多余变量的使用。为了方便理解,_可以看作被丢弃的变量名称,这样做可以让阅读你代码的人知道,这是...

python占位符输入方式实例

占位符,顾名思义就是插在输出里站位的符号。占位符是绝大部分编程语言都存在的语法, 而且大部分都是相通的, 它是一种非常常用的字符串格式化的方式。 1、常用占位符的含义 s : 获取传入...

python数据结构之二叉树的遍历实例

遍历方案    从二叉树的递归定义可知,一棵非空的二叉树由根结点及左、右子树这三个基本部分组成。因此,在任一给定结点上,可以按某种次序执行三个操作: &nb...

使用Python的Tornado框架实现一个简单的WebQQ机器人

我打算将WebQQ单独出来运行, 一开始直接拷贝了pyxmpp2的mainloop, 但是跑起来问题多多, 所以我又研究了利用Tornado进行网络编程(这里), 所以我放弃了Pyxmp...