zoukankan      html  css  js  c++  java
  • Python---进阶---Tkinter---game

    一、用tkinter写一个小游戏,来随机生成我们需要的名字

    # 用tkinter写一个小游戏,来随机生成我们需要的名字

    import tkinter as tk
    import random

    window = tk.Tk()

    def random_1():
    s1 = ['cats', 'hippos', 'cakes']
    s = random.choice(s1)
    return s

    def random_2():
    s2 = ['eats', 'likes', 'hates', 'has']
    s = random.choice(s2)
    return s

    def button_click():
    name = nameEntry.get()
    verb = random_1()
    noun = random_2()
    sentence = name + "" + verb + "" + noun
    result.delete(0, tk.END)
    result.insert(0, sentence)

    nameLabel = tk.Label(window, text="Name:")
    nameEntry = tk.Entry(window)
    button = tk.Button(window, text='生成随机名称', command=button_click)
    result = tk.Entry(window)

    nameLabel.pack()
    nameEntry.pack()
    button.pack()
    result.pack()


    window.mainloop()
    二、写一个输入密码的小程序,我们自己设定一个密码,如果用户输入正确则显示 正确,否则,显示不正确
    # 写一个输入密码的小程序,我们自己设定一个密码,如果用户输入正确则显示 正确,否则,显示不正确

    # encoding:utf-8

    import tkinter as tk
    window = tk.Tk()

    def check_password():
    password = '123456'
    entered_password = passwordEntry.get()
    if password == entered_password:
    confirmLabel.config(text="正确")
    else:
    confirmLabel.config(text="不正确")


    passwordLabel = tk.Label(window, text="Password: ")
    passwordEntry = tk.Entry(window, show="*")
    button = tk.Button(window, text="校验", command=check_password)
    confirmLabel = tk.Label(window)

    passwordLabel.pack()
    passwordEntry.pack()
    button.pack()
    confirmLabel.pack()


    window.mainloop()

    三、
    # encoding:utf-8
    # 一个猜数字的小游戏,让计算机随机生成一个整数,用户输入去猜这个整数,
    # 如果用户输入正确,那么我们分数加1,并且显示计算机生成的数字
    # 如果用户没有输入正确,那么我们的分数不变,还是要显示计算机生成的数字

    import random
    import tkinter as tk
    window = tk.Tk()

    maxno = 10
    score = 0
    rounds = 0

    def button_click():
    global score
    global rounds

    try:
    guess = int(guessBox.get())
    if 0 < guess <= maxno:
    result = random.randrange(1, maxno+1)
    if guess == result:
    score += 1
    rounds += 1
    else:
    result = "输入不合法"
    except:
    result = "输入不合法"

    resultLabel.config(text = result)
    scoreLabel.config(text = str(score) + "/" + str(rounds))
    guess.delete(0, tk.END)

    scoreLabel = tk.Label(window)
    resultLabel = tk.Label(window)
    guessBox = tk.Entry(window)
    guessLabel = tk.Label(window, text="请输入1到"+str(maxno))
    button = tk.Button(window, text="guess", command=button_click)

    scoreLabel.pack()
    resultLabel.pack()
    guessBox.pack()
    guessLabel.pack()
    button.pack()

    window.mainloop()
  • 相关阅读:
    jmeter非GUI模式运行单节点
    Feign详细构建过程及自定义扩展
    【springboot 源码解析】springboot 依赖管理
    通知神器——java调用钉钉群自定义机器人
    【乱纪】Markdown中常用的乱七八糟的东西
    【Java入地】 01 多线程与高并发
    【Linux】默认文本编辑器 vim 的入门与进阶
    【Linux】 安装搜狗输入法
    【Linux】系统发展史上的血雨腥风
    【Java进阶】01 Lambda 使用与进阶
  • 原文地址:https://www.cnblogs.com/niaocaizhou/p/11076723.html
Copyright © 2011-2022 走看看