首頁 > 軟體

基於Python編寫一個點名器的範例程式碼

2022-07-01 22:01:00

前言

想起小學的時候老師想點名找小夥伴回答問題的時候,老師竟斥巨資買了個點名器。今日無聊便敲了敲小時候老師斥巨資買的點名器。

本人姓白,就取名小白點名器啦,嘿嘿

程式碼包含:新增姓名、檢視花名冊、使用指南、隨機抽取名字的功能(完整原始碼在最後)

主介面

定義主介面。使用“w+”模式建立test.txt檔案(我新增了個背景圖片,若不需要可省略)

#開啟時預載入儲存在test.txt檔案中的花名冊
namelist = []
with open("test.txt", "r") as f:
    for line in f.readlines():
        line = line.strip('n')
        namelist.append(line)
win = Tk()
win.title('小白點名器')
win.geometry('500x300')
#定義畫布,新增背景圖片
canvas = Canvas(win,width=500,height=300)
img_obj = PhotoImage(file=r"C:UsersgeDownloadsIMG_202206307919_png.png") #需輸入照片路徑
image = canvas.create_image(250,0,anchor = "n" , image = img_obj)
canvas.pack()
a = StringVar()
b = StringVar()
b.set('開始')
#定義可變文字資訊
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()

新增姓名

定義新增姓名介面,每新增一次姓名就儲存到test.txt檔案中,判斷輸入是否為空(新增提示框)、判斷花名冊是否為空。

#定義新增姓名介面
def addname():
    global Entry1
    window = Tk()
    window.title('姓名新增器')
    window.geometry('400x200+500+200')
    Label11 = Label(window, text = '請在下方輸入你要新增的姓名', 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 = '確認', font=('等線', 18), command = addname1).place(x= 300,y =80, height=80)
#每新增一次姓名就儲存到test.txt檔案中
def addname1():
    global namelist #宣告為全域性變數實時更新
    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)

檢視花名冊

這個比較簡單,使用Text來顯示字典內的資訊即可

def chakan():
    window = Tk()
    window.title('花名冊檢視')
    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('小白點名器使用指南')
    window.geometry('350x230+500+200')
    console = Text(window, font=('等線', 11))
    console.place(y=20, x=35, width=280, height=190)
    console.insert(1.0, '                歡迎使用小白點名器1.0n你可以在」新增姓名按鈕上輸入你要新增的名字n你可以在」檢視「按鈕中檢視花名冊中所有的名字'
                        'n你可以在此程式同級的名為」花名冊.txt「的資料夾中直接批次新增、刪減姓名(使用回車做分隔)n--------------------------------n'
                        '(指南之外)此程式在CSDN中已開源,歡迎存取我的部落格:晉升閣n需要合作的可加我微信:baijinge1137')

名字轉動功能

判斷“開始”、“停止”狀態。定義執行緒。啟用一個執行緒

#判斷狀態
def zhuanzhuan():
    if b.get() == '開始':
        b.set('停止')
    elif b.get() =="停止":
        b.set('開始')
    _thread.start_new_thread(xiancheng,()) #啟用一個執行緒來轉動姓名
#定義一個執行緒
def xiancheng():
    global xuanzhong
    while b.get()=='停止':
        try:
            xuanzhong = random.choice(namelist)
            a.set(xuanzhong)
            Label1.updata()#重新整理資料
            time.sleep(0.3)#0.3秒重新整理一次
        except:
            continue
            time.sleep(0.3)
    a.set(xuanzhong)

完整程式碼

提示:我的專案是在主介面新增了背景圖片的,若是不需要新增背景圖片可刪掉90-94行程式碼。若是需要新增背景圖片的需注意路徑地址是否正確

import random
import time
from tkinter import *
import _thread
import tkinter.messagebox
def zhuanzhuan():
    if b.get() == '開始':
        b.set('停止')
    elif b.get() =="停止":
        b.set('開始')
    _thread.start_new_thread(xiancheng,()) #啟用一個執行緒來轉動姓名
 
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 #宣告為全域性變數實時更新
    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('花名冊檢視')
    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('小白點名器使用指南')
    window.geometry('350x230+500+200')
    console = Text(window, font=('等線', 11))
    console.place(y=20, x=35, width=280, height=190)
    console.insert(1.0, '                歡迎使用小白點名器1.0n你可以在」新增姓名按鈕上輸入你要新增的名字n你可以在」檢視「按鈕中檢視花名冊中所有的名字'
                        'n你可以在此程式同級的名為」花名冊.txt「的資料夾中直接批次新增、刪減姓名(使用回車做分隔)n--------------------------------n'
                        '(指南之外)此程式在CSDN中已開源,歡迎存取我的部落格:晉升閣n需要合作的可加我微信:baijinge1137')
 
def addname():
    global Entry1
    window = Tk()
    window.title('姓名新增器')
    window.geometry('400x200+500+200')
    Label11 = Label(window, text = '請在下方輸入你要新增的姓名', 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 = '確認', 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('小白點名器')
win.geometry('500x300')
canvas = Canvas(win,width=500,height=300)
img_obj = PhotoImage(file=r"C:UsersgeDownloadsIMG_202206307919_png.png") #背景圖片路徑,若不需要新增將85—88行刪掉即可
image = canvas.create_image(250,0,anchor = "n" , image = img_obj)
canvas.pack()
a = StringVar()
b = StringVar()
b.set('開始')
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編寫一個點名器的範例程式碼的詳細內容,更多關於Python點名器的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com