python写一个随机点名软件的实例写一个随机点名软件的实例
今天小编就为大家分享一篇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(''):
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()