Recently there is a random name software needs, so write a, on the code:github address
# -*- coding: utf-8 -*- # @Time : 18-12-31 4:21pm # @Author : Felix Wang from tkinter import * import as tkFont import random import gc import os, sys import chardet import copy from threading import Thread import time def resource_path(relative): """ Image path :param relative: :return. """ if hasattr(sys, "_MEIPASS"): return (sys._MEIPASS, relative) return (relative) def center_window(root, width, height): """ Center size :param root: tk object :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) (size) def clear(): (0, END) def get_names(): names = [] name_path = (BASE_DIR, '') with open(name_path, 'rb') as f: data = () cod = (data)['encoding'] if 'gb' in str(cod): cod = 'gbk' for name in (cod).strip().split('\n'): if (): (()) return names class MyThread(Thread): def __init__(self): = False self.auto_choise = False self.is_auto = -1 super().__init__() def run(self): while True: if self.is_auto is True: while : index = (0, len(names) - 1) echo["text"] = names[index] root.update_idletasks() (1 / 23 - 0.003) self.is_auto = -1 elif self.is_auto is False: if self.auto_choise: () self.is_auto = -1 (0.1) def stop(self): """ Action when clicking the stop button in a manual lottery :return. """ if self.is_auto: = False button2["text"] = 'Manual lottery' pict['image'] = huaji_gif _name = (names) echo["text"] = _name (END, _name) root.update_idletasks() () for x in locals().keys(): del locals()[x] () pict['image'] = huaji_gif () def go(self): """ Stop sign when starting manually :return. """ if self.is_auto == -1: self.is_auto = True = True pict["image"] = huang_gif button2["text"] = 'Click to stop' def auto_start(self): """ Automatically start setting change marks :return. """ if self.is_auto == -1: self.is_auto = False self.auto_choise = True pict["image"] = huang_gif button["text"] = 'Don't light me up yet' global ft1 ft1 = (family='Fixdsys', size=80, weight=) def auto(self): """ Actions performed at the start of the autostart :return. """ copy_names = (names) ren = int(()) for i in range(ren): for a in range(23): index = (0, len(names) - 1) echo["text"] = (names) root.update_idletasks() (1 / 23 - 0.003) choise_name = copy_names.pop((range(len(copy_names)))) echo["text"] = choise_name (END, choise_name) if i == ren - 1: pict['image'] = huaji_gif button["text"] = 'Start the lottery' for a in range(5): () (0.06) root.update_idletasks() for x in locals().keys(): del locals()[x] () () self.auto_choise = False flag = False def name2(): global flag flag = not flag if flag: () else: () def name(): tr.auto_start() try: BASE_DIR = ((__file__)) names = get_names() root = Tk() ("lottery(by Wang Yitao (1942-), Chinese landscape painter and poet)") center_window(root, 570, 160) ft = (family='Fixdsys', size=40, weight=) ft1 = (family='Fixdsys', size=80, weight=) echo = Label(root, text='Random draw', font=ft, width=8) # Default display (row=1, column=1, columnspan=2) scrolly = Scrollbar(root) (row=1, column=5, rowspan=2, ipady=30) lb = Listbox(root, yscrollcommand=, exportselection=False, height=6) (row=1, column=3, rowspan=2, columnspan=2, pady=0) scrolly['command'] = # button = Button(root, text='Delete selected name', command=lambda x=lb: (ACTIVE)) # (row=3, column=3) button = Button(root, text='Delete all names', command=clear) (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) # Pictures at the raffle data_dir = (BASE_DIR, "img") huaji_gif = PhotoImage(file=resource_path((data_dir, ''))) huang_gif = PhotoImage(file=resource_path((data_dir, ''))) pict = Label(root, image=huaji_gif) (row=1, column=0, rowspan=3) button = Button(root, text='Auto-draw', command=name) (row=3, column=1, columnspan=1) flag = False button2 = Button(root, text='Manual lottery', command=name2) (row=3, column=2, columnspan=1) tr = MyThread() (True) () () except Exception as e: print('Error message', e) (60)
The effect is as follows:
The above example of this python to write a random roll call software is all that I have shared with you.