zoukankan      html  css  js  c++  java
  • 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('<Return>',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('<Button-1>',e_btn_guess)#绑定转换按钮,单击事件函数
    btn_reset.pack(side = "left",padx = 10)
    btn_reset.bind('<Button-1>',e_btn_reset)
    btn_change.pack(side = "left",padx = 10)     
    btn_change.bind('<Button-1>',e_btn_change)
    btn_close.pack(side = "left",padx = 10)      #布局关闭按钮
    btn_close.bind('<Button-1>',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()
     
     
  • 相关阅读:
    享受生命中的琐碎时光
    探秘无线路由高级选项 化解IP冲突
    企业无线路由器故障案例分析
    网络基础:无线网络技术规范
    optimize table 删除空洞MYSQL
    Python操作excel001(了解excel基本操作
    Python操作excel002(读取数据
    NopCommerce 自定义DataAnnotation DisplayAttribute
    asp.net mvc FluentValidation 的使用
    nopCommerce : Updating an existing entity. How to add a new property.
  • 原文地址:https://www.cnblogs.com/mmit/p/11581621.html
Copyright © 2011-2022 走看看