演示2:python的tkinter制作自动售货机-针对初学者

  • 说明
  • 知识点
  • 效果
  • 源码


说明

本示例使用random、tkinter实现,tk空间参数类似html的参数。可针对初学者学习。

知识点

1.Cavas画布
ca=Cavas(tk,width,height,bg)
tk:tkinter.Tk()
bg:背景色
2.Button按钮
b=Button(tk,text,font,command)
text:按钮文本
font:(‘微软雅黑’,18)–字体名,字体大小
command:按钮点击触发事件
lambda:function(参数)
3.ca.create_line((x1,y1,x2,y2),width,fill)–画直线
x1,y2–起点
x2,y2–终点
width–线条宽度
fill=('blue)–线条颜色
4.ca.create_text((x,y),font)–绘制文本
x,y–文本中心坐标
5.ca.create_rectangle((x1,y1,x2,y2),fill,outline)–画矩形
x1,y1–矩形左上角坐标
x2,y2–矩形右下角坐标
fill–矩形填充颜色
outline–矩形边框颜色
6.ca.create_polygon((x1,y1,x2,y2,x3,y3,…),fill,outline)–画多边形
x1,y1–第一个点坐标
x2,y2–第二个点坐标
x3,y3–第三个点坐标

7.ca.create_oval((x1,y1,x2,y2),fill)–画椭圆、圆
x1,y1–椭圆外接矩形左上角坐标
x2,y2–椭圆外接矩形右下角坐标
8.ca.create_arc((x1,y1,x2,y2),start,extent,style)–画圆
x1,y1–圆外接矩形左上角坐标
x2,y2–圆外接矩形右下角坐标
start–开始角度
extent–绘制弧度
style–风格
ARC:只画弧
CHORD:弧以及弧两终点的连线
PIESLICE:扇形
9.range(0,8)–创建列表
类似[0,1,2,3,4,5,6,7],但不能用它赋值给变量

效果

自动杂志贩售机Python界面框_2d


自动杂志贩售机Python界面框_tkinter_02


自动杂志贩售机Python界面框_tkinter_03

源码

from tkinter import *
import random
selectgoods=[]
selectmoney=0
#样品展示窗背景颜色
colora='#ffffff'
t=Tk()
t.title('自动售货机')
t.minsize(500,800)
t.maxsize(500,800)
ca=Canvas(t,width=505,height=805,bg='#FF7F24')
ca.place(x=-2,y=-2)
ca.create_rectangle((350,-2,360,805),fill='#ffffff',width=0)
ca.create_rectangle((0,740,350,745),fill='#ffffff',width=0)
ca.create_rectangle((360,740,505,745),fill='#ffffff',width=0)
ca.create_text((430,40),text='阿西吧')
#选购商品提示
def selectgoodtip():
    ca.create_rectangle((370,60,490,360),fill='#196ec2')
    ca.create_text((430,70),text='您已选择',font=('微软雅黑',8),fill='#ffffff')
    ca.create_line((380,80,480,80),fill='#ffffff')
    ca.create_line((380, 340, 480, 340), fill='#ffffff')
    ca.create_text((430, 350), text='总计:'+str(selectmoney)+'元', font=('微软雅黑', 8), fill='#ffffff')
    i=0
    for te in selectgoods:
        ca.create_text((430,90+i*15),text=te,font=('微软雅黑',7),fill='#ffffff')
        i=i+1
#操作提示
def operationtip(text):
    ca.create_rectangle((370,370,490,400),fill='#196ec2')
    ca.create_text((430,385),text=text,fill='#ffffff',font=('微软雅黑',8))
#商品选择处理
def addselectfood(name,money):
    global selectmoney,selectgoods
    if len(selectgoods)<17:
        selectgoods.append(name)
        selectmoney= selectmoney+money
        selectgoodtip()
        operationtip('点击确认付款')
    else:
        operationtip('购物上限:确认')
#绘制瓶子 坐标xy 名字 价格 液体颜色,标签颜色,瓶盖颜色
def drawBottle(x,y,name,price,color1='#8B1A1A',color2='blue',color3='#ffffff'):
    #影子
    ca.create_oval((x-5,y+100,x+45,y+110),fill='#999999')
    #上空
    ca.create_polygon((x+10,y+10,x+30,y+10,x+35,y+15,x+5,y+15,x+10,y+10),fill=colora)
    ca.create_line((x + 30, y + 10, x + 35, y + 15))
    ca.create_line((x + 5, y + 15, x + 10, y + 10))
    # 盖子
    ca.create_rectangle((x + 10, y, x + 30, y + 10), fill=color3)
    #上液体
    ca.create_polygon((x+5,y+15,x+35,y+15,x+40,y+40,x+40,y+45,x+38,y+47,x+2,y+48,x,y+45,x,y+40,x+5,y+15),fill=color1)
    ca.create_line((x+35,y+15,x+40,y+40,x+40,y+45,x+38,y+48))
    ca.create_line((x+2,y+48,x,y+45,x,y+40,x+5,y+15))
    #标签
    ca.create_polygon((x+2,y+48,x+38,y+48,x+38,y+83,x+2,y+83,x+2,y+48),fill=color2)
    ca.create_line((x+2,y+48,x+2,y+83))
    ca.create_line((x+38,y+48,x+38,y+83))
    ca.create_text((x+21,y+65),text=name,font=('微软雅黑',8),fill='#ffffff')
    #下液体
    ca.create_polygon((x+2,y+82,x+38,y+82,x+40,y+84,x+40,y+105,x+35,y+110,x+5,y+110,x,y+105,x,y+84,x+2,y+82),fill=color1)
    ca.create_line((x+38,y+82,x+40,y+84,x+40,y+105,x+35,y+110,x+5,y+110,x,y+105,x,y+84,x+2,y+82))
    #价签
    ca.create_rectangle((x,y+105,x+40,y+125),fill='#ffffff')
    ca.create_text((x+20,y+116),font=('微软雅黑',10),text='$'+str(price))
    #按钮
    Button(t,text='选择',width=4,height=1,font=('微软雅黑',6),command=lambda:addselectfood(name,price)).place(x=x+1,y=y+130)
#氮气包装袋 坐标,名字,价格,主体色,斜条色23
def drawBag(x,y,name,price,color1,color2,color3):
    #影子
    ca.create_oval((x-5,y+77,x+65,y+87),fill='#999999')
    #上封口
    ca.create_rectangle((x,y,x+60,y+7),fill=color1)
    #中间
    ca.create_polygon((x,y+7,x+60,y+7,x+59,y+17,x+58,y+27,x+57,y+37,x+57,y+47,x+58,y+57,x+59,y+67,x+60,y+77,\
                       x,y+77,x+1,y+67,x+2,y+57,x+3,y+47,x+3,y+37,x+2,y+27,x+1,y+17,x,y+7),fill=color1)
    # 斜条
    ca.create_polygon((x+1,y+67,x+2,y+57,x+3,y+47,x+59,y+17,x+58,y+27,x+57,y+37,x+1,y+67),fill=color2)
    ca.create_polygon((x,y+77,x+1,y+67,x+57,y+37,x+57,y+47,x,y+77),fill=color3)
    #中间边框
    ca.create_line((x,y+7,x+60,y+7,x+59,y+17,x+58,y+27,x+57,y+37,x+57,y+47,x+58,y+57,x+59,y+67,x+60,y+77,\
                    x,y+77,x+1,y+67,x+2,y+57,x+3,y+47,x+3,y+37,x+2,y+27,x+1,y+17,x,y+7))
    #下封口
    ca.create_rectangle((x, y+77, x + 60, y + 84), fill=color1)
    #名字
    ca.create_text((x+30,y+20),text=name,font=('微软雅黑',8),fill='#ffffff')
    #价签
    ca.create_rectangle((x+10,y+80,x+50,y+100),fill='#ffffff')
    ca.create_text((x+30,y+91),font=('微软雅黑',10),text='$'+str(price))
    #按钮
    Button(t,text='选择',font=('微软雅黑',6),width=4,height=1,command=lambda:addselectfood(name,price)).place(x=x+11,y=y+105)
#固体饮料 坐标 杯盖颜色 杯身颜色
def drawGlass(x,y,name,price,color1,color2):
    #影子
    ca.create_oval((x-5,y+40,x+45,y+55),fill='#999999')
    #盖子
    ca.create_polygon((x+3,y,x+37,y,x+40,y+5,x,y+5,x+3,y),fill=color1,outline='#000000')
    #杯身
    ca.create_polygon((x,y+5,x+40,y+5,x+35,y+50,x+5,y+50,x,y+5),fill=color2,outline='#000000')
    #名字
    ca.create_text((x+20,y+15),text=name,font=('微软雅黑',8),fill='#ffffff')
    #价签
    ca.create_rectangle((x,y+50,x+40,y+70),fill='#ffffff')
    ca.create_text((x+20,y+61),font=('微软雅黑',10),text='$'+str(price))
    #按钮
    Button(t,text='选择',font=('微软雅黑',6),width=4,height=1,command=lambda:addselectfood(name,price)).place(x=x+1,y=y+75)
#展示台
def drawShowcase(x,y):
    ca.create_rectangle((x,y,x+310,y+170),fill='#ffffff')
    ca.create_rectangle((x,y+170,x+310,y+200),fill='#ffffff')
    ca.create_line((x,y+170,x+10,y+130))
    ca.create_line((x+10,y+130,x+300,y+130))
    ca.create_line((x+300,y+130,x+310,y+170))
    ca.create_line((x+10,y+130,x+10,y))
    ca.create_line((x+300,y+130,x+300,y))
drawShowcase(20,20)
drawShowcase(20,220)
drawShowcase(20,420)
drawBottle(50,60,'可乐',3,'#423a29','#27408b','#ffff00')
drawBottle(120,60,'橙汁',3,'#ffa54f','#ffd700','#fffff0')
drawBottle(190,60,'红茶',4,'#a44641','#ffbf14','#ffa957')
drawBottle(260,60,'绿茶',4,'#4a9447','#39dc32','#52eb89')
drawBag(40,285,'薯条',12,'#ff836b','#fff66f','#fcc75a')
drawBag(110,285,'薯片',10,'#f6fc5c','#c2fc5a','#a8dc4c')
drawBag(180,285,'猫耳',15,'#dac8d9','#6c566b','#562954')
drawBag(250,285,'软糖',8,'#9c9ef2','#f2d99c','#847143')
drawGlass(50,515,'咖啡',6,'#2d2d2d','#ccb47b')
drawGlass(120,515,'奶茶',6,'#5b9584','#afe4d5')
drawGlass(190,515,'果珍',5,'#955b94','#f67ef5')
drawGlass(260,515,'麦片',4,'#807e47','#f6f17e')
#出货口
ca.create_rectangle((30,650,320,720),fill='#cccccc')
#口状态
a=Button(t,text='拿取',font=('微软雅黑',8),width=6,height=1)
def isopen(open):
    global a
    if open:
        ca.create_rectangle((35, 655, 315, 715), fill='#000000')
        ca.create_polygon((35,655,315,655,300,690,50,690,35,655),fill='#999999',outline='#000000')
        a['command']=lambda:isopen(False)
        a.place(x=145,y=680)
    else:
        ca.create_rectangle((35,655,315,715),fill='#cccccc')
        a.place(x=145,y=800)
#确认按钮
def buyok():
    global selectmoney, selectgoods
    if selectmoney>0:
        selectgoods=[]
        selectmoney=0
        selectgoodtip()
        operationtip('请选择商品')
        isopen(True)
Button(t,font=('微软雅黑',7),width=8,height=1,text='确认',command=buyok).place(x=395,y=405)
#纸币入口
ca.create_rectangle((400,450,460,470),fill='#dddddd')
ca.create_rectangle((405,458,455,462),fill='#000000')
#刷卡处
ca.create_rectangle((400,480,460,520),fill='#dddddd')
ca.create_rectangle((420,495,440,505),fill='#dddddd')
ca.create_arc((405,475,455,525),start=30,extent=-60,style=ARC)
ca.create_arc((405,475,455,525),start=150,extent=60,style=ARC)
ca.create_arc((410,480,450,520),start=30,extent=-60,style=ARC)
ca.create_arc((410,480,450,520),start=150,extent=60,style=ARC)
#二维码支付
ca.create_rectangle((410,530,450,570),fill='#dddddd')
ca.create_rectangle((413,533,421,541),width=2)
ca.create_rectangle((416,536,418,538),width=2)
ca.create_rectangle((439,533,447,541),width=2)
ca.create_rectangle((442,536,444,538),width=2)
ca.create_rectangle((413,559,421,567),width=2)
ca.create_rectangle((416,562,418,564),width=2)
ca.create_rectangle((439,559,443,563),width=2)
for i in range(0,4):
    for j in range(0,7):
        if random.randint(0,9)>6:
            ca.create_rectangle((423+j*2,533+i*2,424+j*2,534+i*2),width=2)
for i in range(0,7):
    for j in range(0,17):
        if random.randint(0,9)>6:
            ca.create_rectangle((413+j*2,544+i*2,414+j*2,545+i*2),width=2)
for i in range(0,4):
    for j in range(0,7):
        if random.randint(0,9)>6:
            ca.create_rectangle((423+j*2,559+i*2,424+j*2,560+i*2),width=2)
#找零处
ca.create_rectangle((400,580,460,610),fill='#dddddd')
ca.create_rectangle((405,585,455,605))
#初始化
selectgoods=[]
selectgoodtip()
operationtip('请选择商品')
isopen(False)
t.mainloop()