使用Python中的tkinter模块制作一款文字解密游戏。需要使用tkinter中的label模块显示文字解密的提示信息和玩家解密是否成功的反馈信息;使用tkinter中的button模块供玩家点击以获取提示信息和提交答案;使用tkinter的entry模块供玩家写出自己解密的答案。以下是代码:
import Tkinter as tk //引入tkinter模块
root=tk.Tk() //建立具体的实例
root.title('Who is the criminal?') //将此实例命名
label1=tk.Label(root,text="There are 3 criminal suspects (A,B,C).Please choose the crimianl(s).")
label1.pack() //使用label模块写出本次解密的问题
def info1(): //构建第一个button模块触发的函数
global label2
label2=tk.Label(root,text="A can not lie.") //如果点击第一个button,则显示第一个提示信息。
label2.pack()
def delete1(): //构建第二个button模块触发的函数
label2.pack_forget() //触发第二个button会删除第一个button触发的提示信息。
//按照刚才的构建过程再构建2组这样的button按钮
def info2():
global label3
label3=tk.Label(root,text="B is the victim's son.")
label3.pack()
def delete2():
label3.pack_forget()
def info3():
global label4
label4=tk.Label(root,text="C thinks that B is criminal.")
label4.pack()
def delete3():
label4.pack_forget()
//2组按钮构建完毕
def answer(): //构建一个button触发函数判断玩家解密是否正确
ans=entry.get()
if ans=='A':
label5=tk.Label(root,text='You are right!')
if ans=='a':
label5=tk.Label(root,text='You are right!')
else:
label5=tk.Label(root,text='You are wrong!')
label5.pack()
//依次将提示信息button的触发函数导入到button中
button1=tk.Button(root,text="Show inforamtion 1",command=info1)
button1.pack()
button2=tk.Button(root,text="Hide information 1",command=delete1)
button2.pack()
button3=tk.Button(root,text="Show information 2",command=info2)
button3.pack()
button4=tk.Button(root,text="Hide information 2",command=delete2)
button4.pack()
button5=tk.Button(root,text="Show information 3",command=info3)
button5.pack()
button6=tk.Button(root,text="Hide information 3",command=delete3)
button6.pack()
//导入button触发函数完毕
label6=tk.Label(root,text="Show your answer.") //使用label模块提示玩家输入自己的答案
label6.pack()
entry=tk.Entry(root,text=None) //使用entry模块为玩家提供答题板
entry.pack()
button7=tk.Button(root,text='Submit',command=answer) //使用button让玩家提交信息,并判断玩家提交的答案是否正确
button7.pack()
root.mainloop()
相关链接:
我们的公众号:认知无线电
我们的网站:www.lolplayer.club
我们的Github:Yue-Xiong
欢迎关注和指正