最近学习了python的TK窗口函数,制作了一个简单的抽人小程序
运行环境:python3.7
运行效果:
代码结构比较简单
from tkinter import *
from tkinter import messagebox
import random
class Window(object):
def __init__(self):
self.window=Tk()
self.title="抽人小程序"
self.size=(500,200)
def show(self):
def c(): #输入格式判断
#not e1.get()判断是否为空
if not e1.get():
messagebox.showinfo("警告:","请输入序号范围下限!")
elif not e2.get():
messagebox.showinfo("警告:","请输入序号范围上限!")
elif not e3.get():
messagebox.showinfo("警告:","请输入抽取数量!")
elif int(e1.get())>int(e2.get()) or int(e1.get())<=0 or int(e2.get())<=0:
messagebox.showinfo("警告:","请输入正确的上下限!")
elif int(e3.get())<=0:
messagebox.showinfo("警告:","请输入正确的抽取数量!")
else:
d()
def d(): #抽取并使用弹窗输出
List = []
for i in range(int(e3.get())):
List.append(random.randint(int(e1.get()),int(e2.get())))
messagebox.showinfo("恭喜","抽到===>{}".format(List))
##绘制窗口组件
self.window.geometry("%dx%d"%self.size)
self.window.title(self.title)
##添加序号范围输入框(获取抽取范围)
lb1 = Label(self.window, text='请输入序号范围:')
lb1.grid(row=0, column=0)
v1 = StringVar()
e1 = Entry(self.window, textvariable=v1)
e1.grid(row=0, column=1)
lb2 = Label(self.window, text='~')
lb2.grid(row=0, column=2)
v2 = StringVar()
e2 = Entry(self.window, textvariable=v2)
e2.grid(row=0, column=3)
##添加抽取数量输入框
lb3 = Label(self.window, text='需要抽取人数:')
lb3.grid(row=1, column=0)
v3 = StringVar()
e3 = Entry(self.window, textvariable=v3)
e3.grid(row=1, column=1)
lb4 = Label(self.window, text='人')
lb4.grid(row=1, column=2)
##添加按钮,调用函数c
button1 = Button(self.window, text='抽取', command=c)
button1.grid(row=10, column=4, sticky=E)
if __name__ == '__main__':
window=Window()
window.show()