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()
     
     
  • 相关阅读:
    jvisualm 结合 visualGC 进行jvm监控,并分析垃圾回收
    linux 查看服务器cpu 与内存配置
    arthas 使用总结
    selinux contexts 安全上下文的临时更改
    Android 8.1 Doze模式分析(五) Doze白名单及Debug方式
    Window 任意窗口置顶软件Window TopMost Control
    Android ApkToolPlus一个可视化的跨平台 apk 分析工具
    SVN Please execute the 'Cleanup' command.
    Android 如何在64位安卓系统中使用32位SO库
    Android cmd命令查看apk是32位还是64位?
  • 原文地址:https://www.cnblogs.com/mmit/p/11581621.html
Copyright © 2011-2022 走看看