python写一个随机点名软件
最近有个随机点名软件的需求,故写了一个,上代码:github地址
# -*- coding: utf-8 -*-
# @Time : 18-12-31 下午4:21
# @Author : Felix Wang from tkinter import *
import tkinter.font as tkFont
import random
import gc
import os, sys
import chardet
import copy
from threading import Thread
import time def resource_path(relative):
"""
图片路径
:param relative:
:return:
"""
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative)
return os.path.join(relative) def center_window(root, width, height):
"""
中心大小
:param root: tk对象
:param width:
:param height:
:return:
"""
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(size) def clear():
lb.delete(0, END) def get_names():
names = []
name_path = os.path.join(BASE_DIR, 'name.txt')
with open(name_path, 'rb') as f:
data = f.read()
cod = chardet.detect(data)['encoding']
if 'gb' in str(cod):
cod = 'gbk'
for name in data.decode(cod).strip().split('\n'):
if name.strip():
names.append(name.strip())
return names class MyThread(Thread):
def __init__(self):
self.ifdo = False
self.auto_choise = False
self.is_auto = -1
super().__init__() def run(self):
while True:
if self.is_auto is True:
while self.ifdo:
index = random.randint(0, len(names) - 1)
echo["text"] = names[index]
root.update_idletasks()
time.sleep(1 / 23 - 0.003)
self.is_auto = -1
elif self.is_auto is False:
if self.auto_choise:
self.auto()
self.is_auto = -1
time.sleep(0.1) def stop(self):
"""
手动抽奖时点击停止按钮时的操作
:return:
"""
if self.is_auto:
self.ifdo = False
button2["text"] = '手动抽奖'
pict['image'] = huaji_gif
_name = random.choice(names)
echo["text"] = _name
lb.insert(END, _name)
root.update_idletasks()
root.update()
for x in locals().keys():
del locals()[x]
gc.collect()
pict['image'] = huaji_gif
scrolly.update() def go(self):
"""
手动开始时的停止标记
:return:
"""
if self.is_auto == -1:
self.is_auto = True
self.ifdo = True
pict["image"] = huang_gif
button2["text"] = '点击停止' def auto_start(self):
"""
自动开始设置更改标记
:return:
"""
if self.is_auto == -1:
self.is_auto = False
self.auto_choise = True
pict["image"] = huang_gif
button["text"] = '先别点我'
global ft1
ft1 = tkFont.Font(family='Fixdsys', size=80, weight=tkFont.BOLD) def auto(self):
"""
自动开始时执行的操作
:return:
"""
copy_names = copy.deepcopy(names)
ren = int(v.get())
for i in range(ren):
for a in range(23):
index = random.randint(0, len(names) - 1)
echo["text"] = random.choice(names)
root.update_idletasks()
time.sleep(1 / 23 - 0.003) choise_name = copy_names.pop(random.choice(range(len(copy_names))))
echo["text"] = choise_name lb.insert(END, choise_name)
if i == ren - 1:
pict['image'] = huaji_gif
button["text"] = '开始抽奖'
for a in range(5):
root.update()
time.sleep(0.06) root.update_idletasks() for x in locals().keys():
del locals()[x]
gc.collect() scrolly.update()
self.auto_choise = False flag = False def name2():
global flag
flag = not flag if flag:
tr.go()
else:
tr.stop() def name():
tr.auto_start() try:
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) names = get_names() root = Tk() root.title("随机抽奖器(by 王以涛)") center_window(root, 570, 160) ft = tkFont.Font(family='Fixdsys', size=40, weight=tkFont.BOLD)
ft1 = tkFont.Font(family='Fixdsys', size=80, weight=tkFont.BOLD)
echo = Label(root, text='随机抽奖', font=ft, width=8) # 默认显示
echo.grid(row=1, column=1, columnspan=2) scrolly = Scrollbar(root)
scrolly.grid(row=1, column=5, rowspan=2, ipady=30)
lb = Listbox(root, yscrollcommand=scrolly.set, exportselection=False, height=6)
lb.grid(row=1, column=3, rowspan=2, columnspan=2, pady=0)
scrolly['command'] = lb.yview # button = Button(root, text='删除所选名字', command=lambda x=lb: x.delete(ACTIVE))
# button.grid(row=3, column=3)
button = Button(root, text='删除所有名字', command=clear)
button.grid(row=3, column=4) v = StringVar()
Scale(root, from_=1, to=len(names), resolution=1, orient=HORIZONTAL, variable=v).grid(row=2, column=1, columnspan=2) # 抽奖时的图片
data_dir = os.path.join(BASE_DIR, "img")
huaji_gif = PhotoImage(file=resource_path(os.path.join(data_dir, 'huaji.gif')))
huang_gif = PhotoImage(file=resource_path(os.path.join(data_dir, 'huang.gif')))
pict = Label(root, image=huaji_gif)
pict.grid(row=1, column=0, rowspan=3) button = Button(root, text='自动抽奖', command=name)
button.grid(row=3, column=1, columnspan=1)
flag = False button2 = Button(root, text='手动抽奖', command=name2)
button2.grid(row=3, column=2, columnspan=1) tr = MyThread()
tr.setDaemon(True)
tr.start() root.mainloop()
except Exception as e:
print('错误信息', e)
time.sleep(60)
效果如下:


python写一个随机点名软件的更多相关文章
- 用Python写一个随机数字生成代码,5行代码超简单
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 第一步,安装 random 库 random库是使用随机数的Python标准库 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 十行代码--用python写一个USB病毒 (知乎 DeepWeaver)
昨天在上厕所的时候突发奇想,当你把usb插进去的时候,能不能自动执行usb上的程序.查了一下,发现只有windows上可以,具体的大家也可以搜索(搜索关键词usb autorun)到.但是,如果我想, ...
- 【Python】如何基于Python写一个TCP反向连接后门
首发安全客 如何基于Python写一个TCP反向连接后门 https://www.anquanke.com/post/id/92401 0x0 介绍 在Linux系统做未授权测试,我们须准备一个安全的 ...
- Python写一个自动点餐程序
Python写一个自动点餐程序 为什么要写这个 公司现在用meican作为点餐渠道,每天规定的时间是早7:00-9:40点餐,有时候我经常容易忘记,或者是在地铁/公交上没办法点餐,所以总是没饭吃,只有 ...
- python写一个能变身电光耗子的贪吃蛇
python写一个不同的贪吃蛇 写这篇文章是因为最近课太多,没有精力去挖洞,记录一下学习中的收获,python那么好玩就写一个大一没有完成的贪吃蛇(主要还是跟课程有关o(╥﹏╥)o,课太多好烦) 第一 ...
- [py]python写一个通讯录step by step V3.0
python写一个通讯录step by step V3.0 参考: http://blog.51cto.com/lovelace/1631831 更新功能: 数据库进行数据存入和读取操作 字典配合函数 ...
- #写一个随机产生138开头手机号的程序 1.输入一个数量,产生xx条手机号 2.产生的这些手机号不能重复
import randomcount=int(input('请输入你所想要手机号数量:'))prefix='138'for i in range(count): num=random.sample(r ...
- 用python写一个自动化盲注脚本
前言 当我们进行SQL注入攻击时,当发现无法进行union注入或者报错等注入,那么,就需要考虑盲注了,当我们进行盲注时,需要通过页面的反馈(布尔盲注)或者相应时间(时间盲注),来一个字符一个字符的进行 ...
随机推荐
- Java冒泡排序与快速排序笔记
public class Sort { public static void sort() { Scanner input = new Scanner(System.in); int sort[] = ...
- oracle 查询 10题
说明:表数据来自oracle 初始用户之一scott里面的三个初始表:emp,dept,salgrade --1.查询员工表中工资最高的雇员的员工号.员工姓名.工资和部门号. select empno ...
- 从Iterator到async/await
Generator和Async 引言 接触过Ajax请求的会遇到过异步调用的问题,为了保证调用顺序的正确性,一般我们会在回调函数中调用,也有用到一些新的解决方案如Promise相关的技术. 在异步编程 ...
- chrome滚动条颜色尺寸设置
<style> /*chrome滚动条颜色设置*/ *::-webkit-scrollbar { width: 5px; height: 10px; background-color: t ...
- python学习之操作redis
一.Redis安装网上很多可以查找 二.redis-py的安装,使用命令pip install redis. 安装过程中如果产生连接超时的错误,可以使用国内镜像参考如下 豆瓣:pip install ...
- ES5_对象 与 继承
1. 对象的定义 //定义对象 function User(){ //在构造方法中定义属性 this.name = '张三'; this.age = 12; //在构造方法中定义方法: this.ru ...
- Android笔记(三十六) AsyncTask是如何执行的?
在上一个例子中,我们是在LoadImage的onPostExecute中修改的UI,不是说只允许在主线程中修改UI吗?我们看一下源代码是如何操作的. MainActicity.java package ...
- 微信小程序中使用云开发获取openid
微信小程序获取openid 新建一个微信小程序项目 注意要注册一个自己的小程序账号,并有属于自己的appid 点击云开发按钮,自行填入开发环境名称 打开app.js,找到依赖环境 修改为刚才设置的环境 ...
- 宁波市第二届CTF之cripto1
密码学第一题 给的是一个shadow文件,16进制编辑器打开 是一串Linux root用户密码的密文,猜测密码可能就是flag,于是将这一串字符放到文本. linux下爆破用户密码的工具没接触过(还 ...
- Linux硬盘满了,系统速度贼慢,居然是Jenkins.log太大了
用查找命令找出大于1G的文件 find / -size +1G -print 为什么jenkins.log会产生40+G的文件? 以上在Windows上的时候,运行了几个月,也没有发生这种现象? 而在 ...