import tkinter.messagebox import tkinter import random window = tkinter.Tk() window.geometry('800x400') window.title('猜数字游戏') good_price = random.randint(10, 100) input_price = random.randint(1, 100) def sumit(): global good_price if entry.get() == "" or entry.get().isdigit() == False: message_info.set("价格不合理") tkinter.messagebox.askquestion(title='警告', message='输入不合法') if entry.get().isdigit() == False: entry.delete(first=0, last=len(entry.get())) else: quess_price = int(entry.get()) if 10 <= quess_price <= 100 or entry.get().isspace(): if quess_price > good_price: message_info.set("不好意思,您猜的有点大了!") entry.delete(first=0, last=len(entry.get())) elif quess_price < good_price: message_info.set("不好意思,您猜的有点小了!") entry.delete(first=0, last=len(entry.get())) else: message_info.set("恭喜你,猜对了,祝贺成功!1") price.set(f'{good_price}') entry.delete(first=0, last=len(entry.get())) askok = tkinter.messagebox.askokcancel(title="温馨提示", message="是不继续玩!") if askok == True: price.set("***") message_info.set("请输入你猜的价格") good_price = random.randint(10, 100) entry.delete(first=0, last=len(entry.get())) else: message_info.set("价格不合理,请重新输入") tkinter.messagebox.askquestion(title='警告', message='输入不合法,请重新输入') entry.delete(first=0, last=len(entry.get())) # #-------------界面------------------ lable_price_text = tkinter.Label(window, text='当前商品的价格是:', font=('楷体', 15)) lable_price_text.place(x=10, y=10, width=300, height=40) price = tkinter.StringVar(value='***') lable_price = tkinter.Label(window, textvariable=price, font=('楷体', 15)) lable_price.place(x=300, y=10, width=80, height=40) lable_entry_text = tkinter.Label(window, text='请输入10-100的数字:', font=('楷体', 15)) lable_entry_text.place(x=10, y=60, width=300, height=40) lable_img_text = tkinter.Label(window, text='请看下边的图片:', font=('楷体', 15)) lable_img_text.place(x=10, y=110, width=300, height=40) entry = tkinter.Entry(window, show="") entry.place(x=300, y=60, width=80, height=40) img = tkinter.PhotoImage(file="img.png") lable_dec_pic = tkinter.Label(window,image=img) lable_dec_pic.place(x=10, y=160, width=300, height=200) submit_btn = tkinter.Button(window, text="提交", command=sumit) submit_btn.place(x=400, y=60, width=80, height=40) frame = tkinter.Frame(window,bd=2,relief="sunken") frame.place(x=340, y=170, width=300, height=200) message_lable = tkinter.Label(window, text='提示:', font=('楷体', 15)) message_lable.place(x=350, y=180, width=100, height=40) message_info = tkinter.StringVar(value='请输入您猜的价格') message_m = tkinter.Label(window, textvariable=message_info, font=('楷体', 15)) message_m.place(x=350, y=210, width=280, height=80) window.mainloop()
python看图片猜价格游戏,frame 和PhotoImage的使用
于 2024-08-06 12:45:52 首次发布