通用技术 python 问题求助大佬们

lyyyyyyy · 2019年03月01日 · 最后由 我去催饭 回复于 2019年03月01日 · 1271 次阅读

想弄一个抽奖的代码,从网上 copy 了大神的代码,现在想新添加一个功能就是→第一次和第二次点击结束显示都是 A,第三次是随机的。小白修改之后每次都是 A 了,求大佬帮助!!!

import tkinter
import random
import threading
import time


root = tkinter.Tk()
root.title("随机名单")
root.geometry('300x300+400+200')
root.resizable(False, False)
root.flag = True

second = tkinter.Label(root,text='',font = ("宋体", 20,"normal"))
second['fg'] = 'red'
second.place(x=80,y=100,width=150,height=100)

students = ['杨姐姐1', '杨姐姐2', '杨姐姐3', '杨姐姐4', '杨姐姐5', '杨姐姐6']


def switch():
    root.flag = True
    while root.flag:
        i = random.randint(0, len(students) - 1)
        # first['text'] = second['text']
        # second['text'] = third['text']
        second['text'] = students[i]
        # third['text'] = students[i]
        time.sleep(0.05)



def butStartClick():
    t = threading.Thread(target=switch)
    t.start()


btnStart = tkinter.Button(root, text='开始', command=butStartClick)
btnStart.place(x=30, y=30, width=80, height=20)



def btnStopClick():
    root.flag = False
    count = 0
    while True:
        count = count + 1
        if count <= 2:
            second['text'] = "A"
            break



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


root.mainloop()
共收到 8 条回复 时间 点赞

count = 0 放到函数外面,就可以了。

原来年会抽奖都是这么玩的😩 😩 😩

count = 0
def btnStopClick():
root.flag = False
global count
count += 1
if count <= 2:
second['text'] = "A"

源代码是实现什么的 随机输出某一个 students?

count 设置为全局变量

我去催饭 回复

年会,应该要多一个操作,就是已中奖的要去掉,不能重复中奖。

肖军 回复

这代码看上去像是指定某人中奖啊

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册