image.png (29.6 KB, 下载次数: 0)
下载附件
界面
2023-6-6 11:23 上传
image.png (22.82 KB, 下载次数: 0)
下载附件
2023-6-6 11:26 上传
image.png (24.46 KB, 下载次数: 0)
下载附件
2023-6-6 11:27 上传
[color=]from
tkinter
[color=]import
*
[color=]import
random
#
获取文本里的数字
[color=]def
[color=]get_sz
():
mye=en1.get()
[color=]try
:
myn=
[color=]int
(mye)
[color=]except
[color=]ValueError
:
la1.config(
[color=]text
=
[color=]'
[color=]需要输入数字哦!
[color=]'
)
en1.delete(
[color=]0
[color=],
END)
en1.focus()
return False
[color=] except
:
la1.config(
[color=]text
=
[color=]'
[color=]请输入一个
[color=]1-100
[color=]之间的数字!
[color=]'
)
en1.delete(
[color=]0
[color=],
END)
en1.focus()
return False
[color=] if
myn
[color=]100
:
la1.config(
[color=]text
=
[color=]'
[color=]需要输入
[color=]1-100
[color=]之间的数字,超出范围错误!
[color=]'
)
en1.delete(
[color=]0
[color=],
END)
en1.focus()
[color=]else
:
[color=]return
myn
#
我猜按钮功能
[color=]def
[color=]wocai
():
[color=]global
tn
[color=]if
bu1[
[color=]'text'
]==
[color=]'
[color=]重来
[color=]'
:
tn = random.randint(
[color=]1
[color=],
[color=]100
)
myn=get_sz()
[color=]if
myn:
[color=]if
myn==tn:
la1.config(
[color=]text
=
[color=]'
[color=]恭喜你猜对了!
[color=]'
)
en1.delete(
[color=]0
[color=],
END)
en1.focus()
bu1.config(
[color=]text
=
[color=]'
[color=]重来
[color=]'
)
[color=]elif
myn> tn:
la1.config(
[color=]text
=
[color=]'
[color=]你猜的是
[color=]%s
[color=]你猜大了!
[color=]'
%myn)
en1.delete(
[color=]0
[color=],
END)
bu1.config(
[color=]text
=
[color=]'
[color=]再猜
[color=]'
)
en1.focus()
[color=]else
:
la1.config(
[color=]text
=
[color=]'
[color=]你猜的是
[color=]%s
[color=]你猜小了!
[color=]'
%myn)
en1.delete(
[color=]0
[color=],
END)
bu1.config(
[color=]text
=
[color=]'
[color=]再猜
[color=]'
)
en1.focus()
tn=random.randint(
[color=]1
[color=],
[color=]100
)
root=Tk()
root.title(
[color=]'
[color=]猜数字游戏
[color=]'
)
root.geometry(
[color=]'500x200'
)
myfont=(
[color=]"
[color=]宋体
[color=]"
[color=],
[color=]24
)
la1=Label(root
[color=],
[color=]text
=
[color=]'
[color=]请猜
[color=]1-100
[color=]之间的数字
[color=]'
[color=],
[color=]font
=myfont
[color=],
[color=]bg
=
[color=]'#a0a0a0'
)
la1.pack(
[color=]pady
=
[color=]20
[color=],
[color=]fill
=X)
fr1=Frame(root)
fr1.pack(
[color=]pady
=
[color=]20
)
en1=Entry(fr1
[color=],
[color=]font
=myfont
[color=],
[color=]width
=
[color=]10
)
bu1=Button(fr1
[color=],
[color=]font
=(
[color=]"
[color=]宋体
[color=]"
[color=],
[color=]16
)
[color=],
[color=]width
=
[color=]10
[color=],
[color=]text
=
[color=]'
[color=]我猜
[color=]'
[color=],
[color=]command
=wocai)
en1.pack(
[color=]side
=LEFT
[color=],
[color=]anchor
=CENTER)
en1.focus()
bu1.pack(
[color=]side
=LEFT
[color=],
[color=]anchor
=CENTER)
root.mainloop()