找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4451|回复: 1

[求助] python抽奖程序求助

1

主题

1

帖子

1

积分

贫民

积分
1
li375676756 发表于 2018-11-27 15:00:23 | 显示全部楼层 |阅读模式
1威望
大家好,我刚开始学python,想做一个抽奖程序,源代码如下,如果我要在框架内除了“开始”“结束”外在加上3个按钮(一等奖,二等奖,三等奖)点击1等奖按钮就只出现一个随机滚动的人员名单,2等奖按钮出现2个滚动的滚动栏,依次类推。

这是我在网上找的代码,现在1,2,3等奖的按钮跟开始功能键一样,求大神教一下怎么样一个按钮显示不同等人数。一个人只能中奖一次,并且能输出中奖人员名单。


import tkinterimport randomimport threadingimport timeimport csv    #加载csv包便于读取csv文件# 初始化窗口root = tkinter.Tk()root.title("上海克比年会抽奖名单")root.geometry('500x500+400+200')root.resizable(False, False)root.flag = True# 三个Lable标签csv_file=open('c:/12.csv')    #打开csv文件csv_reader_lines = csv.reader(csv_file)   #逐行读取csv文件students = [ ]    #创建列表准备接收csv各行数据for one_line in csv_reader_lines:    students.append(one_line)  # 将读取的csv分行数据按行存入列表‘date’中students.append(one_line)def switch():    root.flag = True    while root.flag:        i = random.randint(0, len(students) - 1)        first['text'] = second['text']        second['text'] = third['text']        third['text'] = students        time.sleep(0.001)first = tkinter.Label(root, text='', font=("宋体", 20, "normal"))first.place(x=100, y=100, width=300, height=100)second = tkinter.Label(root, text='', font=("宋体", 20, "normal"))second['fg'] = 'red'second.place(x=100, y=200, width=300, height=100)third = tkinter.Label(root, text='', font=("宋体", 20, "normal"))third.place(x=100, y=300, width=300, height=100)# 开始按钮def butStartClick():    t = threading.Thread(target=switch)    t.start()#设置几个奖项btnStart = tkinter.Button(root, text='开始', command=butStartClick)btnStart.place(x=20, y=30, width=80, height=20)one = tkinter.Button(root, text='一等奖', command=butStartClick)one.place(x=110, y=30, width=80, height=20)two = tkinter.Button(root, text='二等奖', command=butStartClick)two.place(x=210, y=30, width=80, height=20)three= tkinter.Button(root, text='三等奖', command=butStartClick)three.place(x=310, y=30, width=80, height=20)# 结束按钮def btnStopClick():    root.flag = FalsebutStop = tkinter.Button(root, text='停止', command=btnStopClick)butStop.place(x=410, y=30, width=80, height=20)# 启动主程序root.mainloop()


捕获.PNG
回复

使用道具 举报

0

主题

8

帖子

8

积分

贫民

积分
8
东虫下草 发表于 2018-12-5 07:17:41 | 显示全部楼层
本帖最后由 东虫下草 于 2018-12-13 17:47 编辑

用最麻烦的方式按题主要求做了调整,希望大佬帮忙简化。调整如下:
1、不同奖项对应不同的switch();
2、每次switch,初始化三个lable的值;
3、每次switch后,pop掉对应的值;
4、输出到excel(未实现)

存在问题:
1、switch函数是否可以只用一个函数
2、点击奖项抽奖时,若继续点击奖项抽奖会卡死
3、循环抽取人员时,加个sleep,不然刷得太快,看不清4、我觉得可以加个可配置奖项,每个奖项对应的人数,
5、人数不够时会报错
  1. import tkinter
  2. import random
  3. import threading
  4. import time
  5. import csv

  6. # 加载csv包便于读取csv文件
  7. # 初始化窗口
  8. root = tkinter.Tk()
  9. root.title("测试抽奖")
  10. root.geometry('600x400+200+200')
  11. root.resizable(False, False)
  12. root.flag = True

  13. # 打开csv文件
  14. csv_file = open('C:\\Users\\FJ\\Desktop\\工作簿1.csv')

  15. # 逐行读取csv文件
  16. csv_reader_lines = csv.reader(csv_file)

  17. # 创建列表准备接收csv各行数据
  18. students = []

  19. # 将读取的csv分行数据按行存入列表‘date’中
  20. for one_line in csv_reader_lines:
  21.     students.append(one_line)
  22. print(students)
  23. # 三个Lable标签
  24. def switch1():
  25.     first['text'] = ''
  26.     second['text'] = ''
  27.     third['text'] = ''
  28.     root.flag = True
  29.     while root.flag:
  30.         i = random.sample(range(0, len(students) - 1), 1)
  31.         first['text'] = students[i[0]]
  32.     students.pop(i[0])


  33. def switch2():
  34.     first['text'] = ''
  35.     second['text'] = ''
  36.     third['text'] = ''
  37.     root.flag = True
  38.     while root.flag:
  39.         i = random.sample(range(0, len(students) - 1), 2)
  40.         first['text'] = students[i[0]]
  41.         second['text'] = students[i[1]]

  42.     for j in range(2):
  43.         if j > 0 and i[j] > i[j-1]:
  44.             students.pop(i[j]-j)
  45.         else:
  46.             students.pop(i[j])


  47. def switch3():
  48.     first['text'] = ''
  49.     second['text'] = ''
  50.     third['text'] = ''
  51.     root.flag = True
  52.     while root.flag:
  53.         i = random.sample(range(0, len(students) - 1), 3)
  54.         first['text'] = students[i[0]]
  55.         second['text'] = students[i[1]]
  56.         third['text'] = students[i[2]]
  57.     for j in range(3):
  58.         if j == 0:
  59.             students.pop(i[j])
  60.         elif j == 1:
  61.             if i[j] > i[j - 1]:
  62.                 students.pop(i[j] - j)
  63.             else:
  64.                 students.pop(i[j])
  65.         else:
  66.             if i[j] > i[j - 1] and i[j] > i[j - 2]:
  67.                 students.pop(i[j] - j)
  68.             elif (i[j - 1] < i[j] < i[j - 2]) or (i[j - 1] > i[j] > i[j - 2]):
  69.                 students.pop(i[j] - j + 1)
  70.             else:
  71.                 students.pop(i[j])

  72.     print(students)

  73. time.sleep(0.001)
  74. first = tkinter.Label(root, text='', font=("宋体", 20, "normal"))
  75. first.place(x=100, y=100, width=300, height=100)
  76. second = tkinter.Label(root, text='', font=("宋体", 20, "normal"))
  77. second['fg'] = 'red'
  78. second.place(x=100, y=200, width=300, height=100)
  79. third = tkinter.Label(root, text='', font=("宋体", 20, "normal"))
  80. third.place(x=100, y=300, width=300, height=100)


  81. # 一等奖
  82. def butStartClick1():
  83.     t = threading.Thread(target=switch1)
  84.     t.start()

  85. # 二等奖
  86. def butStartClick2():
  87.     t = threading.Thread(target=switch2)
  88.     t.start()
  89. # 三等奖
  90. def butStartClick3():
  91.     t = threading.Thread(target=switch3)
  92.     t.start()

  93. # 设置几个奖项
  94. one = tkinter.Button(root, text='一等奖', command=butStartClick1)
  95. one.place(x=110, y=30, width=80, height=20)

  96. two = tkinter.Button(root, text='二等奖', command=butStartClick2)
  97. two.place(x=210, y=30, width=80, height=20)

  98. three = tkinter.Button(root, text='三等奖', command=butStartClick3)
  99. three.place(x=310, y=30, width=80, height=20)


  100. # 结束按钮
  101. def btnStopClick():
  102.     root.flag = False

  103. butStop = tkinter.Button(root, text='停止', command=btnStopClick)
  104. butStop.place(x=410, y=30, width=80, height=20)


  105. # 启动主程序
  106. root.mainloop()
复制代码








回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表