• 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()
     
     
  • 相关阅读:
    从零开始学安全(七)●Linux基础命令学习笔记
    从零开始学安全(六)●黑客常用的Dos命令
    ABAP
    ABAP modify screen:修改屏幕,实现隐藏、禁止输入字段
    C#以对象为成员的例子
    C#构造函数、属性的应用
    C#属性方法 构造函数(不知道自己理解的对不对)
    C#斐波那契数列求法(比较阶乘和循环所用时间)
    C#函数的递归
    C#函数的参数传递2(refout)
  • 原文地址:https://www.cnblogs.com/mmit/p/11581621.html
Copyright © 2020-2023  润新知