鱼C论坛

 找回密码
 立即注册
查看: 1509|回复: 9

抽奖游戏运行一次只能抽一次,请问如何把他改成运行一次能抽10次或者无限次

[复制链接]
发表于 2023-4-9 08:21:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
抽奖游戏运行一次只能抽一次,请问如何把他改成运行一次能抽10次或者无限次
谢谢

import tkinter
import time
import threading


class choujiang:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('抽奖')
        self.root.minsize(300, 300)
        self.isloop = False
        self.newloop = False
        self.setwindow()
        self.root.mainloop()

    def setwindow(self):
        self.btnstart = tkinter.Button(self.root, text="start/stop", command=self.newtask)
        self.btnstart.place(x=90, y=125, width=80, height=50)

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

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

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

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

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

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

        self.btn7 = tkinter.Button(self.root, text="girl", bg='#ccffff')
        self.btn7.place(x=230, y=230, width=50, height=50)

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

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

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

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

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

        self.list1 = [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.isloop = False
                return
            time.sleep(0.1)
            for each in self.list1:
                each['bg'] = 'white'
            self.list1[i]['bg'] = 'white'
            time.sleep(0.000001)
            self.list1[i]['bg'] = 'red'
            i += 1
            if i >= len(self.list1):
                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()

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-9 08:44:52 | 显示全部楼层
要让抽奖游戏运行一次能抽10次或者无限次,需要对程序进行如下修改:

在 class choujiang 中添加计数器 self.count,初始值为0。
修改 newtask 方法,在按钮被点击后,将 self.count 加 10 或者加 1,表示可以抽奖的次数增加了。
修改 rounds 方法,在 while 循环中增加一个判断条件 self.count > 0,即还有剩余的抽奖次数,才会执行抽奖操作,并将 self.count 减 1。
修改后的代码如下:
import tkinter
import time
import threading


class choujiang:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('抽奖')
        self.root.minsize(300, 300)
        self.isloop = False
        self.newloop = False
        self.setwindow()
        self.count = 0
        self.root.mainloop()

    def setwindow(self):
        self.btnstart = tkinter.Button(self.root, text="start/stop", command=self.newtask)
        self.btnstart.place(x=90, y=125, width=80, height=50)

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

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

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

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

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

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

        self.btn7 = tkinter.Button(self.root, text="girl", bg='#ccffff')
        self.btn7.place(x=230, y=230, width=50, height=50)

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

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

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

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

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

        self.list1 = [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 self.count > 0: # 修改的地方
            if self.newloop == True:
                self.isloop = False
                return
            time.sleep(0.1)
            for each in self.list1:
                each['bg'] = 'white'
            self.list1[i]['bg'] = 'white'
            time.sleep(0.000001)
            self.list1[i]['bg'] = 'red'
            i += 1
            if i >= len(self.list1):
                i = 0
            self.count -= 1 # 修改的地方

    def newtask(self):
        if self.isloop == False:
            if self.count == 0: # 修改的地方
                self.count = 10 # 修改的地方
            t = threading.Thread(target=self.rounds)
            t.start()
            self.isloop = True
        elif self.isloop == True:
            self.isloop = False
            self.newloop = True


c = choujiang()
以上修改后,点击 start/stop 按钮一次将会增加可以抽奖的次数,如果要无限抽奖可以直接修改方法 newtask 中的 self.count = 0 改为 self.count = float('inf') 或者其他较大的数字即可。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-9 09:47:03 | 显示全部楼层
歌者文明清理员 发表于 2023-4-9 08:44
要让抽奖游戏运行一次能抽10次或者无限次,需要对程序进行如下修改:

在 class choujiang 中添加计数器  ...

哥,我复制了你给的代码还是只能运行一次呀,而且抽奖结果直接锁定在了同一结果上了,再帮我看看,谢谢哥
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 09:52:38 | 显示全部楼层
本帖最后由 isdkz 于 2023-4-9 10:01 编辑

我这个代码中将 self.newloop 重置为 False 实现无数次抽奖,有什么问题可以提出来
import tkinter
import time
import threading


class choujiang:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('抽奖')
        self.root.minsize(300, 300)
        self.isloop = False
        self.newloop = False
        self.setwindow()
        self.root.mainloop()

    def setwindow(self):
        self.btnstart = tkinter.Button(self.root, text="start", command=self.newtask)                              # 改了这里的text
        self.btnstart.place(x=90, y=125, width=80, height=50)

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

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

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

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

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

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

        self.btn7 = tkinter.Button(self.root, text="girl", bg='#ccffff')
        self.btn7.place(x=230, y=230, width=50, height=50)

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

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

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

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

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

        self.list1 = [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.isloop = False
                self.newloop = False                              # 加了这里
                return
            time.sleep(0.1)
            for each in self.list1:
                each['bg'] = 'white'
            self.list1[i]['bg'] = 'white'
            time.sleep(0.000001)
            self.list1[i]['bg'] = 'red'
            
            i += 1
            if i >= len(self.list1):
                i = 0

    def newtask(self):
        if self.isloop == False:
            t = threading.Thread(target=self.rounds)
            t.start()
            self.btnstart['text'] = 'stop'                              # 加了这里
            self.isloop = True
        elif self.isloop == True:
            self.isloop = False
            self.newloop = True
            time.sleep(1)                              # 加了这里
            self.setwindow()                           # 加了这里


c = choujiang()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 09:53:47 | 显示全部楼层
import tkinter
import time
import threading
import random


class choujiang:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('抽奖')
        self.root.minsize(300, 300)
        self.isloop = False
        self.newloop = False
        self.setwindow()
        self.result = tkinter.StringVar()
        self.result.set('开始抽奖')
        self.count = 0
        self.root.mainloop()

    def setwindow(self):
        self.btnstart = tkinter.Button(self.root, text="开始", command=self.newtask)
        self.btnstart.place(x=40, y=60, width=80, height=50)

        self.btnend = tkinter.Button(self.root, text="停止", command=self.endtask)
        self.btnend.place(x=180, y=60, width=80, height=50)

        self.label_result = tkinter.Label(self.root, textvariable=self.result, font=('Arial', 24), fg='red')
        self.label_result.place(x=70, y=150)

    def randomRun(self):
        i = 0
        list1 = [" "," "," "," "," "," "," "," "," "," "," "," "]
        while self.isloop == True:
            if self.newloop == True:
                self.isloop = False
                return
            time.sleep(0.1)
            index = random.randint(0,11)
            self.result.set(list1[index])
            i += 1
            if i >= 50:
                self.isloop = False

    def newtask(self):
        if self.isloop == False:
            self.count += 1
            t = threading.Thread(target=self.randomRun)
            t.start()
            self.isloop = True

    def endtask(self):
        self.isloop = False
        self.result.set("恭喜您," + str(self.count) + "次后,抽中了" + self.result.get() + "奖品!")
        self.count = 0


c = choujiang()
在这个版本中,我将抽奖的逻辑进行了重新设计。在 randomRun 方法中,使用 random.randint(0,11) 函数随机生成一个整数,然后让抽奖结果显示在窗口上。在每次点击“开始”按钮后,会在开启一个新的线程来运行 randomRun 方法,直到点击“停止”按钮时结束线程,并将抽奖结果显示在窗口上。此外,我还添加了一些界面设计,可以根据需要进行修改。

希望这个版本可以解决你的问题,如果还有其他问题,欢迎继续提问哦~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-9 18:09:35 | 显示全部楼层
歌者文明清理员 发表于 2023-4-9 09:53
在这个版本中,我将抽奖的逻辑进行了重新设计。在 randomRun 方法中,使用 random.randint(0,11) 函数随机 ...

运行结果,看不懂,
C:\Users\Administrator\AppData\Local\JetBrains\PyCharm2022.1\demo\PyCharmLearningProject\venv\Scripts\python.exe C:/Users/Administrator/Desktop/BaiduSyncdisk/py/抽奖修改版2.py
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\BaiduSyncdisk\py\抽奖修改版2.py", line 57, in <module>
    c = choujiang()
        ^^^^^^^^^^^
  File "C:\Users\Administrator\Desktop\BaiduSyncdisk\py\抽奖修改版2.py", line 14, in __init__
    self.setwindow()
  File "C:\Users\Administrator\Desktop\BaiduSyncdisk\py\抽奖修改版2.py", line 27, in setwindow
    self.label_result = tkinter.Label(self.root, textvariable=self.result, font=('Arial', 24), fg='red')
                                                              ^^^^^^^^^^^
AttributeError: 'choujiang' object has no attribute 'result'

进程已结束,退出代码1


                               
登录/注册后可看大图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 18:54:20 | 显示全部楼层
Aiden_H 发表于 2023-4-9 18:09
运行结果,看不懂,
C:%users\Administrator\AppData\Local\JetBrains\PyCharm2022.1\demo\PyCha ...

看起来程序运行时出现了一个错误,提示 AttributeError: 'choujiang' object has no attribute 'result',这个错误原因可能是因为在初始化 choujiang 类时,没有正确地定义 self.result 这个实例变量。

请检查你的代码中是否存在以下几种情况:

检查代码中是否有定义 self.result 的语句,例如 self.result = tkinter.StringVar()。
检查代码中是否有使用 self.result 的地方,例如 self.result.set('开始抽奖')。
如果以上两者都没问题,可以尝试重启 PyCharm 或更换 Python 环境重新运行程序。
如果还不行,可以把你的完整代码复制到文本编辑器中,然后按照上面的步骤逐个排查错误。希望能帮助你解决问题~

另外:发图片:https://fishc.com.cn/thread-226351-1-1.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-9 22:56:36 | 显示全部楼层
歌者文明清理员 发表于 2023-4-9 18:54
看起来程序运行时出现了一个错误,提示 AttributeError: 'choujiang' object has no attribute 'result' ...

import tkinter
import time
import threading
import random


class choujiang:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('抽奖')
        self.root.minsize(300, 300)
        self.isloop = False
        self.newloop = False
        self.setwindow()
        self.result = tkinter.StringVar()
        self.result.set('开始抽奖')
        self.count = 0
        self.root.mainloop()

    def setwindow(self):
        self.btnstart = tkinter.Button(self.root, text="开始", command=self.newtask)
        self.btnstart.place(x=40, y=60, width=80, height=50)

        self.btnend = tkinter.Button(self.root, text="停止", command=self.endtask)
        self.btnend.place(x=180, y=60, width=80, height=50)

        self.label_result = tkinter.Label(self.root, textvariable=self.result, font=('Arial', 24), fg='red')
        self.label_result.place(x=70, y=150)

    def randomRun(self):
        i = 0
        list1 = [" "," "," "," "," "," "," "," "," "," "," "," "]
        while self.isloop == True:
            if self.newloop == True:
                self.isloop = False
                return
            time.sleep(0.1)
            index = random.randint(0,11)
            self.result.set(list1[index])
            i += 1
            if i >= 50:
                self.isloop = False

    def newtask(self):
        if self.isloop == False:
            self.count += 1
            t = threading.Thread(target=self.randomRun)
            t.start()
            self.isloop = True

    def endtask(self):
        self.isloop = False
        self.result.set("恭喜您," + str(self.count) + "次后,抽中了" + self.result.get() + "奖品!")
        self.count = 0


c = choujiang()


我是复制5楼的代码直接运行的,没有修改过
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-9 22:57:56 | 显示全部楼层
歌者文明清理员 发表于 2023-4-9 18:54
看起来程序运行时出现了一个错误,提示 AttributeError: 'choujiang' object has no attribute 'result' ...

我是复制你5楼的代码,没作任何修改
import tkinter
import time
import threading
import random


class choujiang:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('抽奖')
        self.root.minsize(300, 300)
        self.isloop = False
        self.newloop = False
        self.setwindow()
        self.result = tkinter.StringVar()
        self.result.set('开始抽奖')
        self.count = 0
        self.root.mainloop()

    def setwindow(self):
        self.btnstart = tkinter.Button(self.root, text="开始", command=self.newtask)
        self.btnstart.place(x=40, y=60, width=80, height=50)

        self.btnend = tkinter.Button(self.root, text="停止", command=self.endtask)
        self.btnend.place(x=180, y=60, width=80, height=50)

        self.label_result = tkinter.Label(self.root, textvariable=self.result, font=('Arial', 24), fg='red')
        self.label_result.place(x=70, y=150)

    def randomRun(self):
        i = 0
        list1 = [" "," "," "," "," "," "," "," "," "," "," "," "]
        while self.isloop == True:
            if self.newloop == True:
                self.isloop = False
                return
            time.sleep(0.1)
            index = random.randint(0,11)
            self.result.set(list1[index])
            i += 1
            if i >= 50:
                self.isloop = False

    def newtask(self):
        if self.isloop == False:
            self.count += 1
            t = threading.Thread(target=self.randomRun)
            t.start()
            self.isloop = True

    def endtask(self):
        self.isloop = False
        self.result.set("恭喜您," + str(self.count) + "次后,抽中了" + self.result.get() + "奖品!")
        self.count = 0


c = choujiang()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 23:02:54 | 显示全部楼层
Aiden_H 发表于 2023-4-9 22:57
我是复制你5楼的代码,没作任何修改

这我就不知道了,init方法里有reslt的啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 21:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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