程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

python图形化编程:猜数字的游戏(python,猜数字游戏)

hfteth 2025-05-08 18:33:48 技术文章 2 ℃

import random

num = random.randint(1,500)

running = True

times = 0 ##总的次数

from tkinter import* ##导入所有tkinter的函数

root = Tk() ##root表示主窗口

root.title("猜价钱游戏") ##标题

root.resizable(0,0) ##表示框体的X,Y方向可以自由拉升不,1为可以。0为不可以

root.geometry('300x150') ##框体大小设置

def onClick1():

global num ##全局变量

global times

times = times+1

if int(w.get())== num:

result = "你猜对了"

elif int(w.get())>num:

result = "偏高"

else:

result = "偏低"

dis2.config(text = result+" 第%d次"%times)

dis1 = Label(root,text = "请输入一个1-1000之间的数",height = 2, width =24, fg = "black")

##长宽,字体颜色,背景颜色

w = Entry(root) ##单行输入框

button1 = Button(root,text = "提交",fg = 'red',command = onClick1,height = 1, width =8,)

dis2= Label(root,height = 2, width =20,)

dis1.pack()

w.pack()

button1.pack()

dis2.pack()

root.mainloop() ##进入消息循环,必须要有这个函数

最近发表
标签列表