问题:有一个饮料自动售货机(处理单价为5角钱)的控制处理软件,若投5角钱的硬币,按下“橙汁”或“啤酒”的按钮,则相应的饮料就送出来。若投入1元钱的硬币,同样也是按“橙汁”或“啤酒”的按钮,
则自动售货机在送出相应饮料的同时退回5角钱的硬币。
from tkinter import * from tkinter import messagebox def F1(): if(v1.get()==1): if(v2.get()==1): text2.insert("insert","5角") text1.insert("insert","橙汁") elif(v2.get()==2): text2.insert("insert","5角") text1.insert("insert","啤酒") else: messagebox.showinfo("提示","请选择饮料") elif(v1.get()==2): if(v2.get()==1): text2.insert("insert","不找零") text1.insert("insert","橙汁") elif(v2.get()==2): text2.insert("insert","不找零") text1.insert("insert","啤酒") else: messagebox.showinfo("提示","请选择饮料") else: messagebox.showinfo("提示","请投币!") def root_update(): v1.set(3) v2.set(3) text1.delete('1.0',END) text2.delete('1.0',END) #建立窗口root root=Tk() #窗口标题 root.title("自动售卖机") #窗口背景 root.configure(background="#F8F8FF") #设置窗口大小且不可变 root.geometry('400x400') root.resizable(0,0) #设置关联变量 v1=IntVar() v2=IntVar() #选择标签用于恢复选择按钮 Label1=Radiobutton(root,text='请投币:',bg='red',width="10",height="2",variable=v1,value=3) Label1.pack() Label1.place(x=0,y=0) Label2=Radiobutton(root,text='选择饮料:',bg='red',width="10",height="2",variable=v2,value=3) Label2.pack() Label2.place(x=0,y=110) Label3=Label(root,text='请取饮料:',bg='red',width="10",height="2") Label3.pack() Label3.place(x=0,y=300) Label4=Label(root,text='找零:',bg='red',width="10",height="2") Label4.pack() Label4.place(x=200,y=300) #花钱选择 r1=Radiobutton(root,text='1元',variable=v1,value=1) r1.pack() r1.place(x=100,y=50) r2=Radiobutton(root,text='5角',variable=v1,value=2) r2.pack() r2.place(x=200,y=50) #饮料选择 r3=Radiobutton(root,text='橙汁',variable=v2,value=1) r3.pack() r3.place(x=100,y=150) r4=Radiobutton(root,text='啤酒',variable=v2,value=2) r4.pack() r4.place(x=200,y=150) #进行操作按钮 button1=Button(root,text='确认',width='5',height='2',command=F1) button1.place(x=100,y=200) button2=Button(root,text='复位',width='5',height='2',command=root_update) button2.place(x=200,y=200) text1 = Text(root,width=6,height='3') text1.pack() text1.place(x=100, y=300) text2 = Text(root,width=6,height='3') text2.pack() text2.place(x=300, y=300) root.mainloop()
复位: