专业编程基础技术教程

网站首页 > 基础教程 正文

Python tkinter 点名工具

ccvgpt 2024-12-12 11:09:08 基础教程 1 ℃

使用Python tkinter 练习制作一个点名工具

效果图


Python tkinter 点名工具


代码如下

main.py文件

import tkinter as t
import random as r
import time
import math
def clickStart():
    print("clickStart")
    global mListNames
    buttonStart.config(state=t.DISABLED)
    count = 10
    over = 0
    while count>0:
        name = r.choice(mListNames)
        over = over + 0.2
        currentContent.set(name)
        time.sleep(0.2)
        count = count - 0.2
        temp = math.ceil(count) # floor 向下舍入
        currentNum.set(temp)
        windows.update()
    buttonStart.config(state=t.NORMAL)

def readList():
    file = open("config.txt", "r", encoding="utf-8")
    listNames = []
    for line in file:
        listNames.append(line.strip())
    return listNames

windows = t.Tk()
windows.title("点名工具")
windows.geometry("800x500")
# 读取相关人名  文件读取
mListNames = readList()
print(mListNames)
# labelTitle = t.Label(windows, text="点名工具", font=('楷体', 20, "bold"))
# labelTitle.pack()
currentNum = t.StringVar()
currentNum.set("10")
labelNum = t.Label(windows, textvariable=currentNum, font=('楷体', 40, "bold"),width=20,height=2)
labelNum.pack()
currentContent = t.StringVar()
labelContent = t.Label(windows, anchor="center", textvariable=currentContent, bg='yellow', font=('楷体', 70, "bold",), width=10,height=2)
labelContent.pack()
buttonStart = t.Button(windows, text="开始", font=('楷体', 20, "bold"),width=20, height=2, command=clickStart)
buttonStart.pack(padx=5, pady=5)
currentContent.set("请开始")
windows.mainloop()

姓名文件:config.txt



运行

把两文件放同目录下,运行py文件即可


最近发表
标签列表