博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
源代码一
阅读量:5278 次
发布时间:2019-06-14

本文共 3262 字,大约阅读时间需要 10 分钟。

class randomMachine(object):    import random as rd    import tkinter as tk    from tkinter import messagebox    def setWeight(self, weight):        for k, v in weight.items():            for t in range(v):                self.chanceList.append(k)     def drawing(self):        if (len(self.chanceList) <= 0):            self.messagebox.showwarning('ERROR', '奖品数不够')        else:            r = self.rd.randrange(0, len(self.chanceList))  # 随机数            # print("随机数 : ", r)            self.rd.shuffle(self.chanceList)  # 打乱列表            res = self.chanceList.pop(r)            self.messagebox.showinfo('开奖结果', res)            print(res)     def addPrize(self):        try:            if self.inputName.get() != None and self.inputName.get() != '':                if self.inputChance.get()!= '' and isinstance(int(self.inputChance.get()), int):                    self.Chance = int(self.inputChance.get())                    self.Name = self.inputName.get()                    self.weight[self.Name] = self.Chance                    self.setWeight(self.weight)                    self.weight = {}                    self.messagebox.showinfo('SUCCESS', '添加奖品成功!')                    self.inputName.delete(first=0, last='end')                    self.inputChance.delete(first=0, last='end')                else:                    self.messagebox.showwarning('ERROR', '请输入一个整数!')            else:                self.messagebox.showwarning('ERROR', '请输入奖品名称!')         except Exception as e:            self.messagebox.showwarning(e)     def checkPrize(self):        if (len(self.chanceList) <= 0):            self.messagebox.showinfo('ERROR', '没有奖品了')        else:            store = {}            for tmp in self.chanceList:                if store.get(tmp)!=None:                    store[tmp] += 1                else :                    store[tmp] = 1            message = ''            for k, v in store.items():                message +=  k + '剩余 : ' + str(v)+ '个'+'\n'             self.messagebox.showinfo('查询结果', message)     def graphicsUI(self):        self.window = self.tk.Tk(className='抽奖')  # 窗口         self.nameTitle = self.tk.Label()        self.nameTitle["text"] = "在此输入奖品名称"        self.nameTitle.pack()         self.inputName = self.tk.Entry()        self.inputName.pack()         self.chanceTitle = self.tk.Label()        self.chanceTitle["text"] = "在此输入奖品个数"        self.chanceTitle.pack()         self.inputChance = self.tk.Entry()        self.inputChance.pack()         self.addButton = self.tk.Button()        self.addButton["text"] = '添加'        self.addButton["command"] = self.addPrize        self.addButton.pack(side='left', padx=30)         self.checkButton = self.tk.Button()        self.checkButton["text"] = '查询剩余奖品'        self.checkButton["command"] = self.checkPrize        self.checkButton.pack(side='left')         self.drawButton = self.tk.Button()        self.drawButton["text"] = '开奖'        self.drawButton["command"] = self.drawing        self.drawButton.pack(side='right', padx=30)        self.window.mainloop()    def __init__(self):        self.weight = {}        self.chanceList = []if __name__ == "__main__":    test = randomMachine()    test.graphicsUI()

 

转载于:https://www.cnblogs.com/gameforpeace/p/11027621.html

你可能感兴趣的文章
poj3320 Jessica's Reading Problem(尺取思路+STL)
查看>>
分布式计算开源框架Hadoop介绍
查看>>
安卓平台接口剖析
查看>>
坏的事情不都会带来坏的结果
查看>>
RPC的基础:调研EOS插件http_plugin
查看>>
第二次团队冲刺第二天
查看>>
bzoj 2257 (JSOI 2009) 瓶子与燃料
查看>>
11)Java abstract class 和 interface
查看>>
使用xrdp或Xmanager 远程连接 CentOS6
查看>>
Linux误删恢复
查看>>
Unity调用Windows窗口句柄,选择文件和目录
查看>>
HashMap循环遍历方式
查看>>
React Native 入门 调试项目
查看>>
C# 通过 Quartz .NET 实现 schedule job 的处理
查看>>
关于java之socket输入流输出流可否放在不同的线程里进行处理
查看>>
目前为止用过的最好的Json互转工具类ConvertJson
查看>>
Day13
查看>>
tensorflow saver简介+Demo with linear-model
查看>>
Luogu_4103 [HEOI2014]大工程
查看>>
Oracle——SQL基础
查看>>