利用python+tkinter开发一个点名软件

最近上课学生多名字记不住,名册忘记了带,要点名怎么办,好久没有写代码了,于是自己写了个点名软件,记录下吧,第一次接触TK也不是太熟悉,写的不太好,记录下源代码 以后遇到要写桌面软件还是可以耍耍的。

 

tk:文档  https://wiki.python.org/moin/TkInter

tk是python 自带的一个GUI模块

效果:

 

背景图:

 

icon图标:

 

源码:

from win32com.client import Dispatch
from tkinter import *
import tkinter as tk
from PIL import Image
from PIL import ImageTk
import os
import re
import random
from threading import Thread
import pythoncom
import time

stu_path = "名单.txt"  # 学生名单路径


def speaker(str):
    """
    语音播报
    :param str: 需要播放语音的文字
    """
    speaker = Dispatch("SAPI.SpVoice")
    speaker.Speak(str)


class Rollllcall():
    def __init__(self):
        self.win = Tk()
        self.win.title("Python课堂点名器")
        self.win.iconbitmap("image/icon.ico")
        self.win.geometry("750x450")
        self.win.resizable(False, False)  # 不允许放大窗口,避免放大导致布局变形带来的麻烦
        self.start = False  # 开始按钮的状态
        # 增加背景图片
        img = Image.open('image/back.jpg')
        img = ImageTk.PhotoImage(img, size=(650, 450))
        theLabel = tk.Label(self.win,  # 绑定到一个框架
                            # justify=tk.LEFT,  # 对齐方式
                            image=img,  # 加入图片
                            compound=tk.CENTER,  # 关键:设置为背景图片
                            font=("华文行楷", 20),  # 字体和字号
                            fg="white",
                            )  # 前景色
        theLabel.place(x=0, y=0, relwidth=1, relheight=1)
        self.var = tk.StringVar()  # 储存文字的类
        self.var.set("别紧张")  # 设置文字
        NameLabel = tk.Label(self.win, textvariable=self.var,  # 绑定到一个框架
                             justify=tk.LEFT,  # 对齐方式
                             compound=tk.CENTER,  # 关键:设置为背景图片
                             font=("华文行楷", 35),  # 字体和字号
                             fg="SeaGreen",
                             width=10,
                             )  # 前景色
        NameLabel.place(x=280, y=100)

        # 多选框
        self.checkVar = IntVar()
        Checkbutton(self.win, text="语音播放", variable=self.checkVar,
                    onvalue=1, offvalue=0, height=0, width=0).place(x=170, y=410)
        tk.Button(self.win, text='编辑学生名单', height=0, width=0, command=self.pop_win).place(x=520, y=408)

        self.theButton = tk.Button(self.win, text="开始", font=("华文行楷", 13), fg="SeaGreen", width=20,
                                   command=self.callback)
        self.theButton.place(x=300, y=360)  # 调整按钮的位置
        self.win.mainloop()

    def save_names(self, pop, t):
        """
        保存名单内容
        :param win: #弹出窗
        :param t: 文本框对象

        """
        names = t.get(0.0, "end")
        if re.search("", names):
            textlabel = tk.Label(pop, text="注意:名单不能使用中文逗号分隔", font=("华文行楷", 12),  # 字体和字号
                                 fg="red", )
            textlabel.place(y=190, x=10)
        else:
            with open(stu_path, "w", encoding="utf-8") as f:
                f.write(names)
            pop.destroy()

    # 编辑学生姓名
    def pop_win(self):
        pop = Tk(className='学生名单编辑')  # 弹出框框名
        pop.geometry('450x250')  # 设置弹出框的大小 w x h
        pop.iconbitmap("image/icon.ico")
        pop.resizable(False, False)

        # 用来编辑名单的文本框
        t = tk.Text(pop, width=61, height='10')
        t.place(x=10, y=10)
        # 判断文件存不存在
        result = os.path.exists(stu_path)
        if result:
            # 存在
            with open(stu_path, "r", encoding='utf-8') as f:
                names = f.read().strip("\n\r\t")
                t.insert("end", names)

        textlabel = tk.Label(pop, text="学生名单请以,(英文状态)的逗号分隔:\n如:刘亦菲,周迅", font=("华文行楷", 12),  # 字体和字号
                             fg="SeaGreen", )
        textlabel.place(y=150, x=10)

        # 点击确定保存数据
        tk.Button(pop, text='确定', height=0, width=0, command=lambda: self.save_names(pop, t)).place(y=200, x=340)
        tk.Button(pop, text='取消', height=0, width=0, command=pop.destroy).place(y=200, x=400)

    def callback(self):
        # 改变开始按钮的状态
        self.start = False if self.start else True
        # 开始随机名单之后修改按钮上的文字
        self.theButton["text"] = "就你了"
        # 开启一个子线程去做操作随机名字,以及语言播报
        self.t = Thread(target=self.mod_stu_name, args=(self.var, self.checkVar))
        self.t.start()

    def mod_stu_name(self, var, checkVar):
        # 随机读取名单中的一个
        pythoncom.CoInitialize()  # 子线程中调用win32com 语音播放需要设置这一行
        if not os.path.exists(stu_path):
            var.set("请添加名单")
            return None
        with open(stu_path, "r", encoding="utf-8") as f:
            names = f.read().strip("\n\t\r,")
        if not names:
            var.set("请添加名单")
            return None
        name_list = names.split(",")

        random_name = ""
        while self.start:
            random_name = random.choice(name_list)
            var.set(random_name)  # 设置名字随机出现
            time.sleep(0.1)
        self.theButton["text"] = "开始"  # 选中之后将按钮重新修改成 开始
        # 语音播报
        if checkVar.get() == 1:
            speaker(random_name)


if __name__ == '__main__':
    Rollllcall()

 

转载于:https://www.cnblogs.com/huangguifeng/p/10962859.html

这是一个VB6的IDE插件(Addin),使用VB6的IDE直接设计Python的界面。 Python和VB都是能让人快乐的编程语言,我使用了Python之后,很多自己使用的工具都使用Python开发或改写了,因为最终实现的Python代码实在太短了(相比VB),有时候Python一行代码就可以实现VB一个函数的功能。 Python就是这种让人越用越开心的语言。 不过说实在,使用Python开发GUI界面还是麻烦了一些了,自带的标准库Tkinter使用起来非常简单,不过对于习惯了VB拖放控件完成界面设计的偶来说,还是不够人性化。TK也有一个工具叫GUI Builder,不过它使用Layout布局,不够直观,而且用起来很不爽。。 至于PyQt/wxPythonGUI库,尽管有可视化设计工具,但总感觉做一般的轻量级应用是杀鸡用牛刀, 而且不够环保,不够低碳,要带一个很大的库,需要目标机器上夜同样安装了PyQt/wxPython,做不了绿色软件。 所以最终的结果是我更喜欢Tkinter,用起来很简单,绿色环保,真正的跨平台,一个py文件到处运行(担心泄密就编译成pyc)。 很多人都认为TK的界面不够美观,不过我经过多次实验后发现导入Python自带的标准TTK主题库,界面非常Native,不输PyQt/wxPython。 此Addin默认启用TTK支持,也可选择关闭。 总而言之,轻量级GUI,TK+TTK足够。 使用此Addin,你可以不用写一句代码就可以生成一个完整可运行的PythonGUI界面,支持2.X和3.X。 安装方法:将压缩包解压到你希望的目录,然后执行Setup.exe完成注册插件过程,打开VB6就可以用了。 在VB窗体上设计完成界面后(你可以大胆的设置各控件的属性,Addin尽量将其翻译为tkinter的控件属性),点工具栏上的VisualTkinter(图标为一片羽毛),再点'生成代码'按钮,即可生成可运行的Python代码,可以拷贝至剪贴板或保存至文件。 一般情况下你可以不用再改变tkinter的控件属性,但是如果你熟悉tkinter,需要更多的控制,可以一一核对各属性,并且修改,再生成代码。 当然除了用来设计界面外,此ADDIN内置的各控件属性列表可以做为编程参考,比较完整,除了极少数我认为大多数人都不用的属性外,属性定义基本上是我从官方的tkinter文档直接翻译的。 如果还没有VB6,网上找一个VB6精简版即可,不到20M,小巧玲珑。 代码已经在Github上托管,更新的版本可以在这上面找到,需求也可以在上面提: https://github.com/cdhigh/Visual-Tkinter-for-Python
python开发的真实星空显示软件 含真实恒星位置数据3144颗 代码讲解见: https://blog.csdn.net/xiaorang/article/details/106598307 数据格式例: {'long': 0.023278328898474372, 'lat': -0.09961466705757636, 'light': 46, 'const': 66}, {'long': 0.024870941840919196, 'lat': 0.2338062439126301, 'light': 55, 'const': 62}, {'long': 0.028107061526797, 'lat': 1.1204335039257496, 'light': 56, 'const': 18}, {'long': 0.03660100303760025, 'lat': 0.5077259659824991, 'light': 21, 'const': 1}, {'long': 0.04004802831028905, 'lat': 1.0323574005393255, 'light': 23, 'const': 18}, {'long': 0.03944444109507185, 'lat': 0.3178583859888262, 'light': 55, 'const': 62}, {'long': 0.040797071265367454, 'lat': -0.488478858963941, 'light': 54, 'const': 74}, {'long': 0.0410661312228549, 'lat': -0.798444499556106, 'light': 39, 'const': 64}, {'long': 0.043800486202076855, 'lat': 0.1945266317121166, 'light': 55, 'const': 66}, {'long': 0.045036755271142, 'lat': 0.804111967609767, 'light': 50, 'const': 1}, {'long': 0.043785947609407745, 'lat': -1.4350775693910554, 'light': 53, 'const': 58}, {'long': 0.04915283505929031, 'lat': -0.2699684886295715, 'light': 49, 'const': 21}, {'long': 0.050498187206605094, 'lat': -0.4851966800391031, 'light': 54, 'const': 74}, {'long': 0.05119631890740283, 'lat': -0.6131874860342564, 'light': 52, 'const': 74}, {'long': 0.05775584219505068, 'lat': 0.26500400429202875, 'light': 28, 'const': 62}, {'long': 0.05896303407877759, 'lat': 0.7162006931179011, 'light': 57, 'const': 1}, {'long': 0.06371905629046214, 'lat': 0.3526728525507925, 'light': 48, 'const': 62}, {'long': 0.06387905062299246, 'lat': -0.33043929519585447, 'light': 44, 'const': 21}, 代码解说详细的教程见: https://blog.csdn.net/xiaorang/article/details/106598307
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值