第五節
身份證產生器
我們先來做一個簡單的視窗
python random參考:http://www.runoob.com/python/func-number-random.html
給大家複製貼上
cityId=["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "I", "O"]
cityValue=["10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
#引入random
import random
cityId=["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "I", "O"]
cityValue=["10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
cityRnd=random.randint(0,25)
print(cityId[cityRnd])
print(cityValue[cityRnd])
#引入random
import random
cityId=["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "I", "O"]
cityValue=["10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
#乘法規則
multi=[1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
cityRnd=random.randint(0,25)
print(cityId[cityRnd])
print(cityValue[cityRnd])
#產生性別
sex=random.randint(1,2)
#隨機生成一個7位數字
middle=random.randint(1000000,9999999)
newId=cityValue[cityRnd]+str(sex)+str(middle)
print(newId)
index=0
for num in newId:
result=int(num)*multi[index]
print(num+"*"+str(multi[index]))
index+=1
#這是最後定稿的身份證產生程式
#引入random
import random
cityId=["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "I", "O"]
cityValue=["10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
#乘法規則
multi=[1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
cityRnd=random.randint(0,25)
print(cityId[cityRnd])
print(cityValue[cityRnd])
#產生性別
sex=random.randint(1,2)
#隨機生成一個7位數字
middle=random.randint(1000000,9999999)
newId=cityValue[cityRnd]+str(sex)+str(middle)
finalId=cityId[cityRnd]+str(sex)+str(middle)
print(newId)
index=0
allplus=0
for num in newId:
result=int(num)*multi[index]
print(num+"*"+str(multi[index]))
allplus+=result
print(allplus)
index+=1
if allplus%10==0:
finalId=finalId+"0"
else:
endNum=10-allplus%10
finalId=finalId+str(endNum)
print(finalId)
#最後就是把一開始視窗的程式和後面產生身份證字號的程式結合在一起
import tkinter as tk
import random
window = tk.Tk()
window.title('身份證產生器')
window.geometry('200x100')
def hit_me():
cityId=["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "W", "Z", "I", "O"]
cityValue=["10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
#乘法規則
multi=[1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
cityRnd=random.randint(0,25)
#產生性別
sex=random.randint(1,2)
#隨機生成一個7位數字
middle=random.randint(1000000,9999999)
newId=cityValue[cityRnd]+str(sex)+str(middle)
finalId=cityId[cityRnd]+str(sex)+str(middle)
index=0
allplus=0
for num in newId:
result=int(num)*multi[index]
allplus+=result
index+=1
if allplus%10==0:
finalId=finalId+"0"
else:
endNum=10-allplus%10
finalId=finalId+str(endNum)
var.set(finalId)
# 这里是窗口的内容
var = tk.StringVar() # 这时文字变量储存器
l = tk.Label(window,
textvariable=var, # 使用 textvariable 替换 text, 因为这个可以变化
bg='green', font=('Arial', 12), width=15, height=2)
l.pack()
b = tk.Button(window,
text='hit me', # 显示在按钮上的文字
width=15, height=2,
command=hit_me) # 点击按钮式执行的命令
b.pack() # 按钮位置
window.mainloop()
應觀眾要求,把寫好的程式打包成exe
1.到命令提示字元輸入「pip install pyinstaller」
2.在命令提示字元輸入「D:」
3.在命令提示字元輸入「cd 109」
4.在命令提示字元輸入「pyinstaller -F 20200731.py」