猜数字游戏(Tkinter)

通过做一个猜数字的小游戏来学习tkinter模块,3次机会

 

功能:

1. 界面

2. 运行结果

 

 

知识点:

1. tkinter中的Lable,Entry,Button, Text组件

2. from tkinter import messagebox

程序代码:

 

'''
GussNumberGame
'''
import tkinter as tk
import tkinter.messagebox
#s1: creat window
GussNum = tk.Tk()
GussNum.minsize(400,300)
GussNum.title('MyGameGussNumber')

#s2: creat components  and
#s3: distribute components
lableShow = tk.Label(GussNum,text='Wellcome to our game!\nplease input yourNumber',height =2)
lableShow.place(x=50,y=10)
entryIn = tk.Entry(GussNum)
entryIn.place(x=50,y=70)
cmdGuss = tk.Button(GussNum,text='Guess')
cmdGuss.place(x=230,y=70)
txtOut = tk.Text(GussNum)
txtOut.place(x=10,y=100)

#s4:events
from random import randint
targetNum = randint(1,11)
count = 0
flagOver = False
def checkNum():
    global targetNum
    global count
    global  flagOver
    yourNum = eval(entryIn.get())
    txt = ''
    txt1 = ''
    if yourNum == targetNum:
        flagOver = True
        txt = "Congratulations! good job!"
        tk.messagebox.showinfo('Good',txt)
    elif yourNum < targetNum:
        txt = 'too low!'
        txt1 = 'You Can try'+ str(2-count)+'times'
        tk.messagebox.showinfo("low",txt)
    else:
        txt = 'too height!'
        txt1 = 'You Can try'+ str(2-count)+'times'
        tk.messagebox.showinfo("height",txt)
    txtOut.insert(tk.END,"yourNum is "+str(yourNum)+' '+ txt+txt1+'\n')
    count += 1
    if count >=3:
        flagOver = True

def guessClick():
    global flagOver
    if not flagOver:
        checkNum()

cmdGuss['command']=guessClick

GussNum.mainloop()

 后续问题: 

1. 游戏结束后(flagOver==TRUE),再点击Guess按钮,应该有提示

2. 可以显示图片来对应相应的结果:哭脸,笑脸

4. 界面添加,猜数范围设定,和  猜测次数设定

5. 界面需美化

6. ....

 

转载于:https://www.cnblogs.com/wfsh-hebau/p/10132846.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值