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()
     
     
  • 相关阅读:
    hdu 5387 Clock (模拟)
    CodeForces 300B Coach (并查集)
    hdu 3342 Legal or Not(拓扑排序)
    hdu 3853 LOOPS(概率DP)
    hdu 3076 ssworld VS DDD(概率dp)
    csu 1120 病毒(LICS 最长公共上升子序列)
    csu 1110 RMQ with Shifts (线段树单点更新)
    poj 1458 Common Subsequence(最大公共子序列)
    poj 2456 Aggressive cows (二分)
    HDU 1869 六度分离(floyd)
  • 原文地址:https://www.cnblogs.com/mmit/p/11581621.html
Copyright © 2011-2022 走看看