tkinter版漂亮的猜数小游戏

tkinter版漂亮的猜数小游戏


以下是部分代码预览:

"""背景图片  背景.png 400x200
   猜猜按钮图片 button_guess.png  100x44
   说明按钮图片 button_explain.png  100x44
   
"""
from tkinter import  messagebox
from tkinter import *
from PIL import ImageTk,Image
from random import randint

        
def explain():
    """单击‘说明’按钮弹出相关字符串。"""
    messagebox.showinfo(game_title, "Hello,我是计算机,欢迎来到猜数小游戏。\n\n我会生成一个从1到100以内的整数,你猜猜它是多少吧。")

def clear_explain(event):
    """文本框的单击鼠标事件会运行这个函数"""
    answer_text.delete(0, END)                          # 清空所有文本
    
    
game_title = "猜数小游戏"

window = Tk()                                           # 新建窗口
window.resizable(width=False,height=False)              # 设置窗口不能变化大小
window.geometry("400x200")                              # 设置窗口的几何尺寸
window.title(game_title)                                # 设置窗口的标题
 
canvas = Canvas(window,width=400,height=200,bg="white") # 创建画布
canvas.pack(expand=True)                                # 放置画布,expand为真

"这是输入数字的文本框"
answer_text = Entry(window,width=12,font=('',13,'normal'),relief=RIDGE,bg= 'orange',fg='black') # 输入数字的文本框
answer_text.insert(0, "在此输入数字")
answer_text.place(x=30,y=120)                           # 放置
answer_text.bind('<Button-1>', clear_explain)
 
tip = canvas.create_text(30,175,text = " ",fill='cyan',font=("Arial", 13, "normal")) # 创建画布上的提示文字

random_number = randint(1,100)                          # 产生随机数         

button_guess = Button(window,width=100 ,command=check,image = button_image_guess)     # 在window上新建 猜猜 按钮,命令为check,图形为button_image_guess
button_guess.place(x=160,y=110)                                                       # 放置

button_explain = Button(window,width=100,command=explain,image = button_image_explain) # 新建说明按钮
button_explain.place(x=280,y=110)

window.mainloop()

如需要查看完整代码,请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。