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()
  • 相关阅读:
    定义类或对象
    CSS 超出的文字显示省略号(单行、多行)
    获取Json对象的长度以及判断json对象是否为空
    第三次作业附加
    八皇后问题解题报告(dfs
    STL学习笔记(不定期更新)
    寒假作业之三
    寒假作业之二(2)
    寒假作业之二(1)
    第一篇随笔居然是总结耶
  • 原文地址:https://www.cnblogs.com/niaocaizhou/p/11076723.html
Copyright © 2011-2022 走看看