亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Python編寫一個(gè)點(diǎn)名器的示例代碼

 更新時(shí)間:2022年07月01日 16:35:52   作者:晉升閣  
想起小學(xué)的時(shí)候老師想點(diǎn)名找小伙伴回答問(wèn)題的時(shí)候,老師竟斥巨資買了個(gè)點(diǎn)名器。今日無(wú)聊便敲了敲小時(shí)候老師斥巨資買的點(diǎn)名器,希望對(duì)大家有幫助

前言

想起小學(xué)的時(shí)候老師想點(diǎn)名找小伙伴回答問(wèn)題的時(shí)候,老師竟斥巨資買了個(gè)點(diǎn)名器。今日無(wú)聊便敲了敲小時(shí)候老師斥巨資買的點(diǎn)名器。

本人姓白,就取名小白點(diǎn)名器啦,嘿嘿

代碼包含:添加姓名、查看花名冊(cè)、使用指南、隨機(jī)抽取名字的功能(完整源碼在最后)

主界面

定義主界面。使用“w+”模式創(chuàng)建test.txt文件(我添加了個(gè)背景圖片,若不需要可省略)

#打開(kāi)時(shí)預(yù)加載儲(chǔ)存在test.txt文件中的花名冊(cè)
namelist = []
with open("test.txt", "r") as f:
    for line in f.readlines():
        line = line.strip('\n')
        namelist.append(line)
win = Tk()
win.title('小白點(diǎn)名器')
win.geometry('500x300')
#定義畫布,添加背景圖片
canvas = Canvas(win,width=500,height=300)
img_obj = PhotoImage(file=r"C:\Users\ge\Downloads\IMG_202206307919_png.png") #需輸入照片路徑
image = canvas.create_image(250,0,anchor = "n" , image = img_obj)
canvas.pack()
a = StringVar()
b = StringVar()
b.set('開(kāi)始')
#定義可變文本信息
Label1 = Label(win, textvariable=a, font=('黑體', 100)).place(y= 60 , x=65)
#定義四個(gè)按鈕
Button1 = Button(win, textvariable=b, font=('等線', 30), command = zhuanzhuan).place(y=210,x = 190)
Button2 = Button(win, text = '添加姓名', font=('等線', 20), command = addname).place(x= 50,y =0)
Button3 = Button(win, text = '查看', font=('等線', 20), command = chakan).place(x= 230,y =0)
Button4 = Button(win, text = '指南', font=('等線', 20), command = zhinan).place(x= 360,y =0)
win.mainloop()

添加姓名

定義添加姓名界面,每添加一次姓名就保存到test.txt文件中,判斷輸入是否為空(添加提示框)、判斷花名冊(cè)是否為空。

#定義添加姓名界面
def addname():
    global Entry1
    window = Tk()
    window.title('姓名添加器')
    window.geometry('400x200+500+200')
    Label11 = Label(window, text = '請(qǐng)?jiān)谙路捷斎肽阋砑拥男彰?, font=('黑體', 18), anchor='center').place(y=30, x=25)
    Entry1 = Entry(window, font=('等線', 30), width=70)
    Entry1.place(y=80, x=70, width=200, height=80)
    Button3 = Button(window, text = '確認(rèn)', font=('等線', 18), command = addname1).place(x= 300,y =80, height=80)
#每添加一次姓名就保存到test.txt文件中
def addname1():
    global namelist #聲明為全局變量實(shí)時(shí)更新
    if len(Entry1.get()) == 0:
        tkinter.messagebox.showinfo('提示', '姓名輸入不能為空哦')
    else:
        if len(Entry1.get()) == 2:
            zhongjian = list(Entry1.get())[::1]
            zhongjian1 = zhongjian[0] + '  ' +zhongjian[1]
            if len(namelist) == 0:
                nam = zhongjian1
            else:
                nam = '\n' + zhongjian1
        else:
            if len(namelist) == 0:
                nam = str(Entry1.get())
            else:
                nam = '\n' + str(Entry1.get())
        with open("test.txt", "a") as f:
            f.write(nam)
        tip = '姓名:' + Entry1.get() + '   添加成功'
        tkinter.messagebox.showinfo('提示', tip)
        print(nam)
        namelist = []
        with open("test.txt", "r") as f:
            for line in f.readlines():
                line = line.strip('\n')
                namelist.append(line)

查看花名冊(cè)

這個(gè)比較簡(jiǎn)單,使用Text來(lái)顯示字典內(nèi)的信息即可

def chakan():
    window = Tk()
    window.title('花名冊(cè)查看')
    window.geometry('350x200+500+200')
    console = Text(window, font=('等線', 11))
    console.place(y=20, x=35, width=280, height=170)
    console.insert(1.0,namelist)

使用指南 

同上,使用Text顯示 

def zhinan():
 
    window = Tk()
    window.title('小白點(diǎn)名器使用指南')
    window.geometry('350x230+500+200')
    console = Text(window, font=('等線', 11))
    console.place(y=20, x=35, width=280, height=190)
    console.insert(1.0, '                歡迎使用小白點(diǎn)名器1.0\n你可以在”添加姓名按鈕上輸入你要添加的名字\n你可以在”查看“按鈕中查看花名冊(cè)中所有的名字'
                        '\n你可以在此程序同級(jí)的名為”花名冊(cè).txt“的文件夾中直接批量添加、刪減姓名(使用回車做分隔)\n--------------------------------\n'
                        '(指南之外)此程序在CSDN中已開(kāi)源,歡迎訪問(wèn)我的博客:晉升閣\n需要合作的可加我微信:baijinge1137')

名字轉(zhuǎn)動(dòng)功能

判斷“開(kāi)始”、“停止”狀態(tài)。定義線程。啟用一個(gè)線程

#判斷狀態(tài)
def zhuanzhuan():
    if b.get() == '開(kāi)始':
        b.set('停止')
    elif b.get() =="停止":
        b.set('開(kāi)始')
    _thread.start_new_thread(xiancheng,()) #啟用一個(gè)線程來(lái)轉(zhuǎn)動(dòng)姓名
#定義一個(gè)線程
def xiancheng():
    global xuanzhong
    while b.get()=='停止':
        try:
            xuanzhong = random.choice(namelist)
            a.set(xuanzhong)
            Label1.updata()#刷新數(shù)據(jù)
            time.sleep(0.3)#0.3秒刷新一次
        except:
            continue
            time.sleep(0.3)
    a.set(xuanzhong)

完整代碼

提示:我的項(xiàng)目是在主界面添加了背景圖片的,若是不需要添加背景圖片可刪掉90-94行代碼。若是需要添加背景圖片的需注意路徑地址是否正確

import random
import time
from tkinter import *
import _thread
import tkinter.messagebox
def zhuanzhuan():
    if b.get() == '開(kāi)始':
        b.set('停止')
    elif b.get() =="停止":
        b.set('開(kāi)始')
    _thread.start_new_thread(xiancheng,()) #啟用一個(gè)線程來(lái)轉(zhuǎn)動(dòng)姓名
 
def xiancheng():
    global xuanzhong
    while b.get()=='停止':
        try:
            xuanzhong = random.choice(namelist)
            a.set(xuanzhong)
            Label1.updata()
            time.sleep(0.3)
        except:
            continue
            time.sleep(0.3)
    a.set(xuanzhong)
 
def addname1():
    global namelist #聲明為全局變量實(shí)時(shí)更新
    if len(Entry1.get()) == 0:
        tkinter.messagebox.showinfo('提示', '姓名輸入不能為空哦')
    else:
        if len(Entry1.get()) == 2:
            zhongjian = list(Entry1.get())[::1]
            zhongjian1 = zhongjian[0] + '  ' +zhongjian[1]
            if len(namelist) == 0:
                nam = zhongjian1
            else:
                nam = '\n' + zhongjian1
        else:
            if len(namelist) == 0:
                nam = str(Entry1.get())
            else:
                nam = '\n' + str(Entry1.get())
        with open("test.txt", "a") as f:
            f.write(nam)
        tip = '姓名:' + Entry1.get() + '   添加成功'
        tkinter.messagebox.showinfo('提示', tip)
        print(nam)
        namelist = []
        with open("test.txt", "r") as f:
            for line in f.readlines():
                line = line.strip('\n')
                namelist.append(line)
 
def chakan():
    window = Tk()
    window.title('花名冊(cè)查看')
    window.geometry('350x200+500+200')
    console = Text(window, font=('等線', 11))
    console.place(y=20, x=35, width=280, height=170)
    console.insert(1.0,namelist)
 
def zhinan():
 
    window = Tk()
    window.title('小白點(diǎn)名器使用指南')
    window.geometry('350x230+500+200')
    console = Text(window, font=('等線', 11))
    console.place(y=20, x=35, width=280, height=190)
    console.insert(1.0, '                歡迎使用小白點(diǎn)名器1.0\n你可以在”添加姓名按鈕上輸入你要添加的名字\n你可以在”查看“按鈕中查看花名冊(cè)中所有的名字'
                        '\n你可以在此程序同級(jí)的名為”花名冊(cè).txt“的文件夾中直接批量添加、刪減姓名(使用回車做分隔)\n--------------------------------\n'
                        '(指南之外)此程序在CSDN中已開(kāi)源,歡迎訪問(wèn)我的博客:晉升閣\n需要合作的可加我微信:baijinge1137')
 
def addname():
    global Entry1
    window = Tk()
    window.title('姓名添加器')
    window.geometry('400x200+500+200')
    Label11 = Label(window, text = '請(qǐng)?jiān)谙路捷斎肽阋砑拥男彰?, font=('黑體', 18), anchor='center').place(y=30, x=25)
    Entry1 = Entry(window, font=('等線', 30), width=70)
    Entry1.place(y=80, x=70, width=200, height=80)
    Button3 = Button(window, text = '確認(rèn)', font=('等線', 18), command = addname1).place(x= 300,y =80, height=80)
 
namelist = []
with open("test.txt", "r") as f:
    for line in f.readlines():
        line = line.strip('\n')
        namelist.append(line)
win = Tk()
win.title('小白點(diǎn)名器')
win.geometry('500x300')
canvas = Canvas(win,width=500,height=300)
img_obj = PhotoImage(file=r"C:\Users\ge\Downloads\IMG_202206307919_png.png") #背景圖片路徑,若不需要添加將85—88行刪掉即可
image = canvas.create_image(250,0,anchor = "n" , image = img_obj)
canvas.pack()
a = StringVar()
b = StringVar()
b.set('開(kāi)始')
Label1 = Label(win, textvariable=a, font=('黑體', 100)).place(y= 60 , x=65)
Button1 = Button(win, textvariable=b, font=('等線', 30), command = zhuanzhuan).place(y=210,x = 190)
Button2 = Button(win, text = '添加姓名', font=('等線', 20), command = addname).place(x= 50,y =0)
Button3 = Button(win, text = '查看', font=('等線', 20), command = chakan).place(x= 230,y =0)
Button4 = Button(win, text = '指南', font=('等線', 20), command = zhinan).place(x= 360,y =0)
win.mainloop()

以上就是基于Python編寫一個(gè)點(diǎn)名器的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python點(diǎn)名器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python并發(fā)2之使用asyncio處理并發(fā)

    python并發(fā)2之使用asyncio處理并發(fā)

    本篇文章主要介紹了python并發(fā)2之使用asyncio處理并發(fā),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 使用python生成目錄樹(shù)

    使用python生成目錄樹(shù)

    這篇文章主要為大家詳細(xì)介紹了使用python生成目錄樹(shù)、文件的程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python實(shí)現(xiàn)屏幕截圖的代碼及函數(shù)詳解

    Python實(shí)現(xiàn)屏幕截圖的代碼及函數(shù)詳解

    本文給大家分享一段關(guān)于python實(shí)現(xiàn)屏幕截圖及函數(shù)的代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-10-10
  • python requests更換代理適用于IP頻率限制的方法

    python requests更換代理適用于IP頻率限制的方法

    今天小編就為大家分享一篇python requests更換代理適用于IP頻率限制的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • Python之 requests的使用(一)

    Python之 requests的使用(一)

    requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),爬蟲(chóng)和測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到,requests是Python語(yǔ)言的第三方的庫(kù),專門用于發(fā)送HTTP請(qǐng)求,使用起來(lái)比urllib簡(jiǎn)潔很多,這篇文章主要介紹requests的基礎(chǔ)用法
    2023-04-04
  • 如何更換python默認(rèn)編輯器的背景色

    如何更換python默認(rèn)編輯器的背景色

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于如何更換python默認(rèn)編輯器的背景色的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2020-08-08
  • python 實(shí)現(xiàn)仿微信聊天時(shí)間格式化顯示的代碼

    python 實(shí)現(xiàn)仿微信聊天時(shí)間格式化顯示的代碼

    這篇文章主要介紹了python 實(shí)現(xiàn)仿微信聊天時(shí)間格式化顯示,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-04-04
  • python中的tcp示例詳解

    python中的tcp示例詳解

    這篇文章主要給大家介紹了關(guān)于python中tcp協(xié)議的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Pytorch數(shù)據(jù)類型與轉(zhuǎn)換(torch.tensor,torch.FloatTensor)

    Pytorch數(shù)據(jù)類型與轉(zhuǎn)換(torch.tensor,torch.FloatTensor)

    這篇文章主要介紹了Pytorch數(shù)據(jù)類型轉(zhuǎn)換(torch.tensor,torch.FloatTensor),之前遇到轉(zhuǎn)為tensor轉(zhuǎn)化為浮點(diǎn)型的問(wèn)題,今天整理下,我只講幾個(gè)我常用的,對(duì)Pytorch數(shù)據(jù)類型轉(zhuǎn)換相關(guān)知識(shí)感興趣的朋友一起看看吧
    2023-02-02
  • python的多元數(shù)據(jù)類型(上)

    python的多元數(shù)據(jù)類型(上)

    這篇文章主要為大家詳細(xì)介紹了python的多元數(shù)據(jù)類型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評(píng)論