程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Write an example code of the roll counter based on python

編輯:Python

Catalog

Preface

main interface

Add name

Check the roster

Use guide  

Name rotation function

Complete code

Preface

I think of the time when the teacher wanted to call the names of children to answer questions in primary school , The teacher spent a lot of money to buy a roll counter . Today, I knocked on the roll counter that my teacher spent a lot of money to buy when I was a child .

My last name is Bai , It's named Xiaobai roll counter , Hey

The code contains : Add name 、 Check the roster 、 Use guide 、 The function of randomly extracting names ( The complete source code is at the end )

main interface

Define the main interface . Use “w+” Pattern creation test.txt file ( I added a background picture , If not required, it can be omitted )

# When opened, the preload is stored in test.txt The roster in the document namelist = []with open("test.txt", "r") as f: for line in f.readlines(): line = line.strip('\n') namelist.append(line)win = Tk()win.title(' Little white roll counter ')win.geometry('500x300')# Define canvas , Add background image canvas = Canvas(win,width=500,height=300)img_obj = PhotoImage(file=r"C:\Users\ge\Downloads\IMG_202206307919_png.png") # You need to enter the photo path image = canvas.create_image(250,0,anchor = "n" , image = img_obj)canvas.pack()a = StringVar()b = StringVar()b.set(' Start ')# Define variable text information Label1 = Label(win, textvariable=a, font=(' In black ', 100)).place(y= 60 , x=65)# Define four buttons Button1 = Button(win, textvariable=b, font=(' Isoline ', 30), command = zhuanzhuan).place(y=210,x = 190)Button2 = Button(win, text = ' Add name ', font=(' Isoline ', 20), command = addname).place(x= 50,y =0)Button3 = Button(win, text = ' see ', font=(' Isoline ', 20), command = chakan).place(x= 230,y =0)Button4 = Button(win, text = ' guide ', font=(' Isoline ', 20), command = zhinan).place(x= 360,y =0)win.mainloop() Add name

Define the add name interface , Every time you add a name, save it to test.txt In file , Judge whether the input is empty or not ( Add a prompt box )、 Judge whether the roster is empty .

# Define the add name interface def addname(): global Entry1 window = Tk() window.title(' Name adder ') window.geometry('400x200+500+200') Label11 = Label(window, text = ' Please enter the name you want to add below ', font=(' In black ', 18), anchor='center').place(y=30, x=25) Entry1 = Entry(window, font=(' Isoline ', 30), width=70) Entry1.place(y=80, x=70, width=200, height=80) Button3 = Button(window, text = ' confirm ', font=(' Isoline ', 18), command = addname1).place(x= 300,y =80, height=80)# Every time you add a name, save it to test.txt In file def addname1(): global namelist # Declared as a global variable and updated in real time if len(Entry1.get()) == 0: tkinter.messagebox.showinfo(' Tips ', ' The name input cannot be empty ') 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 = ' full name :' + Entry1.get() + ' Add success ' tkinter.messagebox.showinfo(' Tips ', tip) print(nam) namelist = [] with open("test.txt", "r") as f: for line in f.readlines(): line = line.strip('\n') namelist.append(line) Check the roster

This is simpler , Use Text To display the information in the dictionary

def chakan(): window = Tk() window.title(' Roster view ') window.geometry('350x200+500+200') console = Text(window, font=(' Isoline ', 11)) console.place(y=20, x=35, width=280, height=170) console.insert(1.0,namelist) Use guide  

ditto , Use Text Show  

def zhinan(): window = Tk() window.title(' Little white roll counter user's Guide ') window.geometry('350x230+500+200') console = Text(window, font=(' Isoline ', 11)) console.place(y=20, x=35, width=280, height=190) console.insert(1.0, ' Welcome to the little white roll call 1.0\n You can ” Enter the name you want to add on the add name button \n You can ” see “ Button to view all the names in the roster ' '\n You can name the same level of this program ” roster .txt“ Add directly in batches in the folder of 、 Delete name ( Use carriage return to separate )\n--------------------------------\n' '( Beyond the guidelines ) This program is available at CSDN Open source in , Welcome to my blog : Promotion Pavilion \n If you need cooperation, please add me on wechat :baijinge1137') Name rotation function

Judge “ Start ”、“ stop it ” state . Define the thread . Enable a thread

# Judge the state def zhuanzhuan(): if b.get() == ' Start ': b.set(' stop it ') elif b.get() ==" stop it ": b.set(' Start ') _thread.start_new_thread(xiancheng,()) # Enable a thread to rotate the name # Define a thread def xiancheng(): global xuanzhong while b.get()==' stop it ': try: xuanzhong = random.choice(namelist) a.set(xuanzhong) Label1.updata()# The refresh data time.sleep(0.3)#0.3 Refresh every second except: continue time.sleep(0.3) a.set(xuanzhong) Complete code

Tips : My project adds background pictures to the main interface , If you don't need to add a background picture, you can delete 90-94 Line code . If you need to add a background picture, please pay attention to whether the path address is correct

import randomimport timefrom tkinter import *import _threadimport tkinter.messageboxdef zhuanzhuan(): if b.get() == ' Start ': b.set(' stop it ') elif b.get() ==" stop it ": b.set(' Start ') _thread.start_new_thread(xiancheng,()) # Enable a thread to rotate the name def xiancheng(): global xuanzhong while b.get()==' stop it ': 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 # Declared as a global variable and updated in real time if len(Entry1.get()) == 0: tkinter.messagebox.showinfo(' Tips ', ' The name input cannot be empty ') 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 = ' full name :' + Entry1.get() + ' Add success ' tkinter.messagebox.showinfo(' Tips ', 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(' Roster view ') window.geometry('350x200+500+200') console = Text(window, font=(' Isoline ', 11)) console.place(y=20, x=35, width=280, height=170) console.insert(1.0,namelist)def zhinan(): window = Tk() window.title(' Little white roll counter user's Guide ') window.geometry('350x230+500+200') console = Text(window, font=(' Isoline ', 11)) console.place(y=20, x=35, width=280, height=190) console.insert(1.0, ' Welcome to the little white roll call 1.0\n You can ” Enter the name you want to add on the add name button \n You can ” see “ Button to view all the names in the roster ' '\n You can name the same level of this program ” roster .txt“ Add directly in batches in the folder of 、 Delete name ( Use carriage return to separate )\n--------------------------------\n' '( Beyond the guidelines ) This program is available at CSDN Open source in , Welcome to my blog : Promotion Pavilion \n If you need cooperation, please add me on wechat :baijinge1137')def addname(): global Entry1 window = Tk() window.title(' Name adder ') window.geometry('400x200+500+200') Label11 = Label(window, text = ' Please enter the name you want to add below ', font=(' In black ', 18), anchor='center').place(y=30, x=25) Entry1 = Entry(window, font=(' Isoline ', 30), width=70) Entry1.place(y=80, x=70, width=200, height=80) Button3 = Button(window, text = ' confirm ', font=(' Isoline ', 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(' Little white roll counter ')win.geometry('500x300')canvas = Canvas(win,width=500,height=300)img_obj = PhotoImage(file=r"C:\Users\ge\Downloads\IMG_202206307919_png.png") # Background image path , If you do not need to add 85—88 Delete the line image = canvas.create_image(250,0,anchor = "n" , image = img_obj)canvas.pack()a = StringVar()b = StringVar()b.set(' Start ')Label1 = Label(win, textvariable=a, font=(' In black ', 100)).place(y= 60 , x=65)Button1 = Button(win, textvariable=b, font=(' Isoline ', 30), command = zhuanzhuan).place(y=210,x = 190)Button2 = Button(win, text = ' Add name ', font=(' Isoline ', 20), command = addname).place(x= 50,y =0)Button3 = Button(win, text = ' see ', font=(' Isoline ', 20), command = chakan).place(x= 230,y =0)Button4 = Button(win, text = ' guide ', font=(' Isoline ', 20), command = zhinan).place(x= 360,y =0)win.mainloop()

The above is based on Python Write the details of the sample code of a roll counter , More about Python Please pay attention to other relevant articles of software development network for the information of roll call !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved