我想在 tkinter 中做一个随机数猜谜游戏

时间:2021-02-25 02:27:38

标签: python tkinter random

我想要在 def tookawhile(): 中使用 tkinter 从 0 到 1000 的更高或更低的游戏,并且我希望它进行 3 轮。当第 3 轮结束时,我希望它说“干得好,现在转到用户,因为没有游戏”,其中文本框被锁定,因为该人无法编辑

import tkinter as tk
import random
import time
from tkinter import *
playing_game = False
def to_make_video():
    global btn1
    text_widget.configure(state='normal')
    msg = "People who are watching go hit that subscribe button and"+\
          " hit that like button also hit that little bell to turn on"+\
          " notifcations"
    text_widget.delete("0.0", "end")
    text_widget.insert("end", msg)
    text_widget.configure(width=25, height=6)
    text_widget.configure(state='disabled')
    btn1.destroy()
    start_game()

def tookawhile():
 text_widget.configure(state='normal',height=4)
 text_widget.delete("0.0","end")
 text_widget.insert("end", "User lets play a game if you arent going to leave\nI have a number between 0 and 1000 in my memory chip can you guess it?")


def whyisthereanapp():
  text_widget.configure(state='normal',width=29,height=2)
  text_widget.delete("0.0","end")
  text_widget.insert("end","Well the creator released it by accident")
  text_widget.configure(state='disabled')
  btn.destroy()
  time.sleep(10)
  tookawhile()
def game_won():
    # When the button is pressed:
    global playing_game
    text_widget.configure(state='normal')
    playing_game = False
    text_widget.delete("0.0", "end")
    text_widget.insert("end", "Why aren't you leaving?")
    text_widget.configure(width=23, height=1)
    text_widget.configure(state='disabled')
    btn.destroy()
    #btn2 = Button(title="I want to play")
    #btn2.pack()

def move_button():
    global playing_game
    # If the game is over stop moving the button
    if not playing_game:
        return None
    # Pick the next random position for the button
    numberx = random.randint(1, 600)
    numbery = random.randint(1, 470)
    btn.place(x=numberx, y=numbery)
    # After 500 milliseconds call `move_button` again
    # You can change the value to make it faster/slower
    root.after(200, move_button)

def start_game():
    # Start the game
    global playing_game
    btn.config(command=game_won)
    playing_game = True
    # Start the loop that keeps moving it to new random positions
    move_button()

def toplayagame():
  text_widget.configure(state='normal')
  text_widget.delete("0.0", "end")
  text_widget.insert("end", "Well there is no game")
  text_widget.configure(width=21)
  text_widget.configure(state='disabled')
  btn1.destroy()
  btn.configure(text='Then why is there an application?',width=25,command=whyisthereanapp,background='Blue',foreground='Yellow',activebackground='Black')
  btn.place(x=349, y=470)
  
def pressed():
    global btn1
    # Ask the user why they are here
    text_widget.configure(state='normal')
    text_widget.delete("0.0", "end")
    text_widget.insert("end", "Why are you here?")
    text_widget.configure(width=17)
    text_widget.configure(state='disabled')
    btn.place(x=190, y=470)
    btn.configure(text="To play a game", width=12, command=toplayagame)

    btn1 = tk.Button(root, bd=10, text="To make a video", bg="grey", fg="white",
                     activebackground="white", activeforeground="black",
                     height=1, width=15, command=to_make_video)
    btn1.place(x=1, y=470)

# Create a window
root = tk.Tk()
root.title("There is no game")
root.geometry("1050x1400")

text_widget = tk.Text(root, height=1, width=10)
text_widget.pack()
text_widget.insert(tk.END, "Hello user")
text_widget.configure(state='disabled')

btn = tk.Button(root, bd=10, text="Hello", activebackground="black",
                activeforeground="white", bg="grey", fg="white", height=1,
                width=4, command=pressed)
btn.place(x=455, y=470)

# Run tkinter's mainloop
root.mainloop()

1 个答案:

答案 0 :(得分:1)

好的,这是一个简单的例子,涉及终端输入,但您可以将其更改为 gui 输入。

`

round = 1
secretNumber = random.randint(1, 1000)
'''text_widget.insert("end",str(secretNumber)) just to know what the number is 
for testing'''
for i in range(3):
    answer = int(input("Enter guess: "))
    if answer < secretNumber:
        print("Higher")
    elif answer > secretNumber:
        print("Lower")
    elif answer == secretNumber:
        print("Correct")
        break
text_widget.config(state=DISABLED)