[Python]實現簡易點名器

點名器

最近在學習Python, 做了一個簡易的點名器, 使用Excel導入點名名單

使用到的庫:

1.xlrd(用於讀取Excel表)

2.random(用於獲取隨機數)

3.tkinter(用於繪製界面)

import random
import tkinter as tk
from tkinter import messagebox

import xlrd

window=tk.Tk()
window.title('Call Name')
window.geometry('300x400')

excel_path = tk.Entry(
    window,
    width=50
)
excel_path.pack()

x_values = []
def load_excel():
    try:
        reader = xlrd.open_workbook(excel_path.get())
        sheet = reader.sheet_by_index(0)
        global x_values 
        x_values = sheet.col_values(0)
    except:
        tk.messagebox.showwarning(title='Warning', message='Read excel Error!')

btn_read_name=tk.Button(
    window,
    text='load',
    width=15,
    height=2,
    command=load_excel
)
btn_read_name.pack(pady=30)

var=tk.StringVar()
lab_show=tk.Label(
    window, 
    textvariable=var,
    bg='black',
    fg='white',
    font=('Arial',12),
    width=15,
    height=2
)
lab_show.pack(pady=30)

def rand_get_name():
    var.set('')
    var.set(x_values[random.randint(0, len(x_values)-1)])

btn_rand_name=tk.Button(
    window,
    text='Click',
    width=15,
    height=2,
    command=rand_get_name
)
btn_rand_name.pack(pady=30)
    
window.mainloop()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章