本文主要是介绍python单词怎么写,Python实现拼写单词的小游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先看一下大概的效果,了解一下小游戏的功能,再看源码
import tkinter as tk
import random
def e_btn_close(event):
root.destroy()
def e_btn_guess(event):
if word == entry_a.get():
result = "恭喜你猜对了!"
else:
result ="很遗憾猜错了!"
textvar.set(result)
def e_btn_reset(event):
entry_a.delete(0,‘end‘)
textvar.set("")
def e_btn_change(event):
global word
word = random.choice(words)
jumble="".join(random.sample(word,len(word)))
textguess.set("您要猜的单词包含字母为:"+jumble)
entry_a.delete(0,‘end‘)
textvar.set("")
words = ["hello","summer","January","February","Marcy","April","May","June",\
"July","August","September","October","November","December"]
word = random.choice(words)
jumble = "".join(random.sample(word,len(word)))
root = tk.Tk(className= "猜单词")
root.geometry("400x200+200+200")
textguess=tk.StringVar()
textguess.set ("您要猜的单词包含字母为:"+jumble)
lable_word=tk.Label(root,width=80,textvariable=textguess)
lable_word.pack(side = "top",anchor = "nw",padx = 10)
root1 = tk.Tk(className= "换一个")
root1.geometry("400x200+200+200")
lable_word = tk.Label(root1,width = 80,text =word)
lable_word.pack(side = "top",anchor = "nw",padx = 10)
entry_a = tk.Entry(root,width = "40") #创建单行文本,
entry_a.pack(side = "top",padx = 10) #布局单行文本框,
entry_a.bind(‘‘,e_btn_guess) #绑定单行文本框中的回车键事件函数
fm1 = tk.Frame(root) #创建子框架窗体
fm1.pack(side = ‘top‘,fill = ‘both‘) #布局子框架窗体
btn_guess = tk.Button(fm1,text = ‘提交‘)#创建转换按钮,在子窗体中
btn_reset = tk.Button(fm1,text ="重置")
btn_change =tk.Button(fm1,text=‘换一个‘)
btn_close =tk.Button(fm1,text=‘关闭‘) #创建关闭按钮,在子窗体中
btn_guess.pack(side = "left",padx = 10) #布局转换按钮
btn_guess.bind(‘‘,e_btn_guess)#绑定转换按钮,单击事件函数
btn_reset.pack(side = "left",padx = 10)
btn_reset.bind(‘‘,e_btn_reset)
btn_change.pack(side = "left",padx = 10)
btn_change.bind(‘‘,e_btn_change)
btn_close.pack(side = "left",padx = 10) #布局关闭按钮
btn_close.bind(‘‘,e_btn_close) #绑定关闭按钮,单击事件函数
# 下面是创建结果标签
textvar = tk.StringVar() #创建容器变量
# 创建结果标签,其中的显示内容为容器变量
lable_conversion = tk.Label(root,width = ‘80‘,textvariable = textvar,anchor = ‘w‘)
lable_conversion.pack(side = ‘top‘,padx = 10) #布局结果标签
entry_a.focus_set() #单行文本框获得焦点
root.mainloop()
原文:https://www.cnblogs.com/mmit/p/11581621.html
这篇关于python单词怎么写,Python实现拼写单词的小游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!