Python 我想在tkinter做一个随机数字猜测游戏
我希望在Python 我想在tkinter做一个随机数字猜测游戏,python,tkinter,random,Python,Tkinter,Random,我希望在def tookawhile():中使用tkinter从0到1000进行更高或更低的游戏,我希望它能进行3轮。当第三轮结束时,我希望它说“做得好,现在去用户,因为没有游戏”,文本框被锁定,因为该人无法编辑它 import tkinter as tk import random import time from tkinter import * playing_game = False def to_make_video(): global btn1 text_widget
def tookawhile():
中使用tkinter从0到1000进行更高或更低的游戏,我希望它能进行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()
这里有一个简单的例子,涉及终端输入,但您可以将其更改为gui输入 `
round=1
secretNumber=random.randint(11000)
''text_widget.insert(“end”,str(secretNumber))只是为了知道数字是什么
用于测试“”“
对于范围(3)中的i:
答案=int(输入(“输入猜测:”)
如果答案机密编号:
打印(“下”)
elif答案==秘密编号:
打印(“正确”)
打破
text_widget.config(状态=禁用)
不太清楚你在问什么,但这里有一些东西可以帮助你text\u widget.config(state=DISABLED)
将禁用文本框import random secretNumber=randint(1,1000)
以获得介于1和1000之间的随机数。@无情我想在tkinter中制作一个更高或更低的游戏,但代码必须放在def tookawhile()下:
你能用另一种格式编写代码吗,因为它太混乱了请不要提问。你已经告诉我们你想要什么,但没有解释你需要什么样的帮助。
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)