|
10鱼币
本帖最后由 mzxay 于 2020-4-15 10:27 编辑
废话少说,先上代码:
- from tkinter import *
- from tkinter import messagebox as g
- import random
- root = Tk()
- root.title("猜数字")
- root.geometry("250x150")
- shu = random.randint(1, 100)
- def ti_jiao():
- a_g = int(a.get())
- if a_g == shu:
- g.showinfo('正确', '恭喜你答对了')
- else:
- if a_g > 100:
- g.showinfo('输入错误','请输入1~100的整数')
- if a_g > shu and a_g < 100 :
- g.showinfo('错误', '你猜大了')
- elif a_g < shu:
- g.showinfo('错误', '你猜小了')
- Label(root, text = '1~100选一个数', bg = 'yellow', fg = 'blue',
- font = ('华文行楷', 15)).place(x = 50, y = 0)
- a = Entry(root, bg = 'yellow', fg = 'blue', font = ('华文行楷', 15))
- a.place(x = 20, y = 50)
- b = Button(root, text='提交', bg = 'yellow', fg = 'blue', font = ('华文行楷', 15), command = ti_jiao)
- b.place(x = 80, y = 90)
- root.mainloop()
复制代码
要求:
1、让用户只有五次猜测机会;
2、如果用户才对后点击确定,将自动退出程序
3、五次没才对将打印:表格一:恭喜你才对了,但当用户点击确定后,再次弹出提示框,打印较大字体:个屁。用户点击确定后也将自动退出程序。
觉得鱼币不够可以加………………
我来我来~~~
- from tkinter import *
- from tkinter import messagebox as m
- import random
- root = Tk()
- root.title("猜数字")
- root.geometry("250x150")
- TIME = 0
- answer = random.randint(1,100)
- def check():
- eg = int(en.get())
- global TIME
- TIME += 1
- if TIME > 5:
- m.showinfo('正确','恭喜你答对了')
- m.showinfo('个屁','个屁')
- root.destroy()
- elif eg == answer:
- m.showinfo('正确','恭喜你答对了')
- root.destroy()
- elif (eg > 100)or(eg < 1):
- m.showinfo('输入错误','请输入1~100的整数')
- elif eg > answer:
- m.showinfo('错误','你猜大了')
- elif eg < answer:
- m.showinfo('错误','你猜小了')
- la = Label(root,text='1~100选一个数',font=('华文行楷',15))
- la.place(x=50,y=0)
- en = Entry(root,font=('华文行楷',15))
- en.place(x=20,y=50)
- bu = Button(root,text='提交',font=('华文行楷',15),command=check)
- bu.place(x=90,y=90)
- root.mainloop()
复制代码
这是我写的
1 你这里少了一次判断(就是 if a_g > 100这句)
2 你12--20行写得不够简洁,应该用 elif
3 标准对话框不能设置字体大小
4 关于次数的话,最好不要用while,我试过, 它会重复五次弹出对话框 ,然后就没有机会再试了
5 对于TIME而言,你应该可以不像我这样做,你可以在函数里面定义,也不用global
|
最佳答案
查看完整内容
我来我来~~~
这是我写的
1 你这里少了一次判断(就是if a_g > 100这句)
2 你12--20行写得不够简洁,应该用elif
3 标准对话框不能设置字体大小
4 关于次数的话,最好不要用while,我试过,它会重复五次弹出对话框 ,然后就没有机会再试了
5 对于TIME而言,你应该可以不像我这样做,你可以在函数里面定义,也不用global
|