首页 > 编程语言 >python随机点名-图片版

python随机点名-图片版

时间:2024-04-23 10:01:16浏览次数:25  
标签:__ 点名 name python self label 随机 ._ image

先创建图片文件夹,图片名就是用户名

from tkinter import *
from PIL import Image, ImageTk
import time,random,os

class RandomName(Frame):
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._timer = None
        self._start = 0.0
        self._elapsedtime = 0.0
        self._running = False
        self.timestr = StringVar()
        self.makeWidgets()

    def makeWidgets(self):
        self.label = Label(self, textvariable=self.timestr, font=("思源宋体", 45), pady=50)
        self.label.pack(side=TOP)
        self.image_label = Label(self)
        self.image_label.pack()
        self.set_name(self._elapsedtime)

    def update(self):
        self._elapsedtime = time.time() - self._start
        self.set_name(self._elapsedtime)
        self._timer = self.after(50, self.update)

    def set_name(self, elap):
        #cur = int(elap * 100 % len(name_list))
        # 从用户列表随机一个
        cur = random.randint(0, len(name_list) - 1)
        username = name_list[cur]
        image_path = os.path.join(images_folder, username + ".png")
        self.timestr.set(username)
        # 设置图片
        image = Image.open(image_path)
        image = image.resize((200, 200))
        photo = ImageTk.PhotoImage(image)
        self.image_label.configure(image=photo)
        self.image_label.image = photo

    def start(self):
        if not self._running:
            self._start = time.time() - self._elapsedtime
            self.update()
            self._running = True
    def stop(self):
        if self._running:
            self.after_cancel(self._timer)
            self._elapsedtime = time.time() - self._start
            self.set_name(self._elapsedtime)
            self._running = False

    def name_label(self):
        self.pack(side=TOP)
        Button(self, text='开始', command=self.start, width=10, height=2, bg="SeaGreen", font=("思源宋体", 13)).pack(
            side=LEFT)
        Button(self, text='结束', command=self.stop, width=10, height=2, bg="Red", font=("思源宋体", 13)).pack(
            side=LEFT)


# 图片文件夹
images_folder = "./students"

# 把用户名抽出来
name_list = [os.path.splitext(img)[0] for img in os.listdir(images_folder) if img.endswith('.png')]

if __name__ == '__main__':
    root = Tk()
    root.title("随机点名")
    root.geometry('400x500')
    root.resizable(0, 0)
    sw = RandomName(root)
    sw.name_label()
    root.mainloop()

标签:__,点名,name,python,self,label,随机,._,image
From: https://www.cnblogs.com/qcy-blog/p/18152193

相关文章

  • python实现随机点名
    新建txt文本,输入名字,每个进行换行fromtkinterimport*importtimeclassRandomName(Frame):def__init__(self,parent=None,**kw):Frame.__init__(self,parent,kw)self._timer=Noneself._start=0.0self._elapsedtime=0......
  • Python遗传算法GA对长短期记忆LSTM深度学习模型超参数调优分析司机数据
    全文链接:https://tecdat.cn/?p=36004原文出处:拓端数据部落公众号随着大数据时代的来临,深度学习技术在各个领域中得到了广泛的应用。长短期记忆(LSTM)网络作为深度学习领域中的一种重要模型,因其对序列数据的强大处理能力,在自然语言处理、时间序列预测等领域中取得了显著的成果。然......
  • Python利用GPU进行深度学习
    在深度学习当中,我们训练模型通常要对模型进行反复的优化训练,仅用CPU来进行训练的话需要花费很长时间,但是我们可以使用GPU来加速训练模型,这样就可以大大减少训练模型花费的时间。 首先我们需要一张NVIDIA显卡在搜索栏中搜索设备管理器前往NVIDIA官网下载显卡对应的Studio......
  • Python企业面试题5 —— 网络编程和并发
    1.简述进程、线程和协程的区别以及应用场景?#进程:拥有自己独立的堆和栈,既不共享堆,也不共享栈,进程由操作系统调度。#线程:拥有自己独立的栈和共享的堆,线程也由操作系统调度。#协程和线程:协程避免了无意义的调度,由此可以提高性能;但同时协程失去了线程使用多CPU的能力。进程与......
  • Mac搭建appium环境及python运行代码示例
    Appium主要是通过调用安卓提供的接口来执行命令的,所以需要安装Java和安卓SDK。1.安装Appium服务端appium的服务端是基于node的,直接使用npm(node包管理器)安装即可,比较简单。npminstall-gappium2.安装Python客户端pipinstallAppium-Python-Client同样直接使用pip安装......
  • python+appium+pytest做app自动化测试
    我在另一篇博客中写了使用unittest做app自动化测试的,包含了前期的环境的环境搭建,请参考如下链接:python+appium+unittest做app自动化测试这里,我们使用pytest框架再改写一个版本,因为pytest做测试报告看着更加好看,代码改良如下:fromappiumimportwebdriverimportpytest@pytest......
  • Python Numpy 矩阵运算
    目录1前言2点积与矩阵乘法2.1np.dot()2.2np.matmul()和@2.3np.multiply和*3矩阵的逆4Ref1前言Python中经常涉及到矩阵运算,其借助于Numpy库进行,因此本文记录一些基于Numpy的矩阵运算2点积与矩阵乘法矩阵的点积(dotproduct),又称为内积(innerproduct)$a=(x_1,y_1)......
  • python+appium+unittest做app自动化测试
    1.需要安装一些列的软件:(1)java(2)androidsdk:  https://www.cnblogs.com/chenxiaomeng/p/16544481.html(3)AppiumServerGUI(4)AppiumInspector (3和4老版本是一个)2.打开AppiumServerGUI直接点击startServer即可,使用默认配置 3.打开 AppiumInspectorremot......
  • python监控MongoDB服务进程,故障钉钉告警
     服务器1xx.168.8x.77#!/usr/bin/python#!_*_coding:utf-8_*_importosimportsysimporttimemongo_ip='192.168.xx.77'ports=['x001','x002']defport(ip,port):  response=os.popen("tcping %s%s|grepopen|awk-F'&......
  • 在Python中的for循环
    在Python中的for循环for循环:用于遍历序列(如列表、元组、字典、集合或字符串)或其他可迭代对象。pythonforiinrange(10):#这将循环10次,i的值从0到9print(i)while循环:当给定条件为真时,重复执行代码块。pythoni=0whilei<10:#这将循环10次print(i)i......