python猜数字GUI版本V0.1

时间:2024-11-14 08:05:02

非常简单的GUI版猜数字游戏,后面有时间好好研究下

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 16:30:17 2019

@author: pelu
"""

import tkinter
import random

number = random.randint(1, 100)

def check_number():
guess_number = int(txt_guess.get())
if guess_number < number:
msg = "You are too low!"
elif guess_number > number:
msg = "You are too high!"
else:
msg = "You win!"
lbl_result["text"] = msg

wd = tkinter.Tk()
wd.title("Guess Number Game")
wd.geometry('600x400')

lbl_title = tkinter.Label(wd, text="I'm thinking of a number between 1 and 100. Take a guess!")
lbl_title.pack()

txt_guess = tkinter.Entry(wd, width=3)
txt_guess.pack()

lbl_result = tkinter.Label(wd,text="Good luck!")
lbl_result.pack()

btn1 = tkinter.Button(wd, text="Submit", fg="red", command=check_number)
btn1.pack()

wd.mainloop()