python实现转盘效果 python实现轮盘抽奖游戏

作者:CAPF_CS 时间:2023-03-06 00:00:57 

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


#抽奖 面向对象版本
import tkinter
import time
import threading

class choujiang:
 #初始化魔术方法
 def __init__(self):
   #准备好界面
   self.root = tkinter.Tk()
   self.root.title('lowB版转盘')
   self.root.minsize(300, 300)
   # 声明一个是否按下开始的变量
   self.isloop = False
   self.newloop = False
   #调用设置界面的方法
   self.setwindow()
   self.root.mainloop()

#界面布局方法
 def setwindow(self):
   #开始停止按钮
   self.btn_start = tkinter.Button(self.root, text = 'start/stop',command = self.newtask)
   self.btn_start.place(x=90, y=125, width=50, height=50)

self.btn1 = tkinter.Button(self.root, text='赵', bg='red')
   self.btn1.place(x=20, y=20, width=50, height=50)

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

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

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

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

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

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

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

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

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

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

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

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

def rounds(self):
   # 判断是否开始循环
   if self.isloop == True:
     return

# 初始化计数 变量
   i = 0
   # 死循环
   while True:
     if self.newloop == True:
       self.newloop = False
       return

# 延时操作
     time.sleep(0.1)
     # 将所有的组件背景变为白色
     for x in self.girlfrends:
       x['bg'] = 'white'

# 将当前数值对应的组件变色
     self.girlfrends[i]['bg'] = 'red'
     # 变量+1
     i += 1
     # 如果i大于最大索引直接归零
     if i >= len(self.girlfrends):
       i = 0

# 建立一个新线程的函数
 def newtask(self):
   if self.isloop == False:
     # 建立线程
     t = threading.Thread(target = self.rounds)
     # 开启线程运行
     t.start()
     # 设置循环开始标志
     self.isloop = True
   elif self.isloop == True:
     self.isloop = False
     self.newloop = True

c = choujiang()

小编再为大家分享一款python模拟轮盘抽奖的游戏

python3.x的版本测试中文的变量名


from random import random
#轮盘赌lpd,奖项分布jxfb,本次转盘读数bclpds,中奖情况zjqk,本次战况bczk,
def lpd(jxfb):
 bclpds = random()
 for k, v in jxfb.items():
   if v[0]<=bclpds<v[1]:
     return k

jxfb = {'一等奖':(0, 0.08),
         '二等奖':(0.08, 0.3),
         '三等奖':(0.3, 1.0)}

zjqk = dict()
#模拟玩10000次,统计中奖情况
for i in range(10000):
 bczk = lpd(jxfb)
 zjqk[bczk] = zjqk.get(bczk, 0) + 1

for item in zjqk.items():
 print(item)

来源:https://blog.csdn.net/weixin_41202652/article/details/78988954

标签:python,转盘,抽奖
0
投稿

猜你喜欢

  • 鼠标双击滚动屏幕单击停止代码

    2008-02-21 11:44:00
  • Python实现文件信息进行合并实例代码

    2021-04-12 01:50:33
  • python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别详解

    2023-03-23 13:16:16
  • 解析数据库分页的两种方法对比(row_number()over()和top的对比)

    2024-01-25 08:58:16
  • 使用python编写一个语音朗读闹钟功能的示例代码

    2021-10-24 13:34:41
  • python导入不同目录下的自定义模块过程解析

    2022-11-08 16:05:47
  • python处理文本文件并生成指定格式的文件

    2021-05-16 15:43:42
  • Python中Selenium模拟JQuery滑动解锁实例

    2021-10-21 09:49:52
  • 利用Golang生成整数随机数方法示例

    2024-05-08 10:22:58
  • Tensorflow2.4使用Tuner选择模型最佳超参详解

    2023-07-19 19:46:22
  • asp中通过addnew添加内容后取得当前文章的自递增ID的方法

    2011-02-05 11:05:00
  • pytest解读fixtures之Teardown处理yield和addfinalizer方案

    2023-06-18 22:13:01
  • Pandas实现DataFrame按行求百分数(比例数)

    2022-10-23 10:49:01
  • 浅谈Python响应式类库RxPy

    2021-12-24 12:44:26
  • Go基础教程系列之import导入包(远程包)和变量初始化详解

    2024-02-22 03:33:53
  • MySQL编码不一致可能引起的一些问题

    2024-01-21 17:26:15
  • Python中的MongoDB基本操作:连接、查询实例

    2021-12-21 07:38:06
  • python实现从文件中读取数据并绘制成 x y 轴图形的方法

    2023-04-09 22:43:41
  • Windows系统下MySQL无法启动的万能解决方法

    2024-01-16 10:59:26
  • 浅析mysql迁移到clickhouse的5种方法

    2024-01-27 21:51:47
  • asp之家 网络编程 m.aspxhome.com