您的位置:首页 > 游戏 > 游戏 > Python Tkinter小程序

Python Tkinter小程序

2024/10/6 4:07:39 来源:https://blog.csdn.net/C_User1024/article/details/141028194  浏览:    关键词:Python Tkinter小程序

我学python有一段时间了,来看一下我用Tkinter编的小程序

一.时钟

from tkinter import *
from time import *
def gettime():time=strftime('%H:%M:%S')l.configure(text=time)root.after(1000,gettime)
root=Tk()
root.title('clock')
root.geometry('500x300')
l=Label(root,text='',bg='black',fg='yellow',font=('等线',100),width=500,height=300)
l.pack()
gettime()
root.mainloop()

第1,2行:导入需要的库

3行:创建函数gettime,用于调取时间

4行:调取时间,格式为 小时:分钟:秒

5行:将l的内容改变为上面调取的时间(见第十行)

6行:在1000毫秒后再次调取时间

7,8,9行:创建窗口,设置标题,设置大小

10行:创建标签l,背景颜色黑色,文本颜色黄色,字体等线,字号100....

11行:放置l

12行:调用函数

13行:刷新

求各位大神在评论区指点一下,非常感谢,要是能指出一些错误就更好了!

二.猜数游戏

from tkinter import *
from random import *
from sys import *
number = randint(100,999)
d = 1
root = Tk()
root.geometry('400x100')
root.title('猜数字游戏')
root['background'] = 'lightblue'
Label(root, text='请输入100到999的整数:', bg='lightblue').place(x=15, y=30)
word = Entry(root, fg='red', width=30)
word.place(x=155, y=30)
def exi():exit()
def okay():try:q = getint(word.get())if q > number:Label(root, text='猜的太大了!', bg='lightblue').place(x=65, y=65)elif q < number:Label(root, text='猜的太小了!', bg='lightblue').place(x=65, y=65)elif q == number:Label(root, text='恭喜!猜对了!', bg='lightblue').place(x=65, y=65)except ValueError:Label(root, text='请输入正确的整数呦~',bg='lightblue').place(x=25,y=65)
def speak():h = '正确答案为:',number,''l = Label(root,text=h,bg='lightblue')l.place(x=175,y=7)Label(root,text='     ',bg='lightblue').place(x=270,y=7)
b = Button(root, text='退出游戏', font=('等线', 10), command=exi)
b.place(x=156, y=55)
b1 = Button(root, text='确认答案', font=('等线', 10), command=okay)
b1.place(x=232, y=55)
b2 = Button(root, text='公布答案', font=('等线', 10),command=speak)
b2.place(x=307, y=55)
root.mainloop()

这个代码有个问题,就是一次只能猜一个数,猜对了不能开始猜下一个,求各位大神指点

1-3行:导入需要的库

4行:随机数

5行是个变量,没用到

6-9行创建一个400x100的窗口,背景颜色为淡蓝

10行:放置一个标签(输入提示语)

11,12行:放置输入数据的文本框

13,14行:退出函数

15-25行:判断输入的数是大了还是小了,如果发生异常,应急处理

26-30行:公布答案的函数

30-36行:3个按钮,对应退出.确认答案,公布答案

37行:刷新

三.贪吃蛇(但是不完善)

这个代码还是有一些问题的,多多指教

我还会改进!

from tkinter import *
b = 0
to = 'east-go'
root = Tk()
root.title('GAME')
root.geometry('850x534')
c = Canvas(root,width=720,height=534)
body1 = c.create_rectangle(20,23,20+50,23+50,fill='#228B22')
body2 = c.create_rectangle(90,23,90+50,23+50,fill='#228B22')
head = c.create_rectangle(160,23,160+50,23+50,fill='#2E8B57')
c.place(x=0,y=0)
Label(root,width=360,bg='black').place(x=0,y=0)
Label(root,width=360,bg='black').place(x=0,y=73)                #Label高23,宽20;方块长50,高50
Label(root,width=360,bg='black').place(x=0,y=146)
Label(root,width=360,bg='black').place(x=0,y=219)
Label(root,width=360,bg='black').place(x=0,y=292)
Label(root,width=360,bg='black').place(x=0,y=365)
Label(root,width=360,bg='black').place(x=0,y=438)
Label(root,width=360,bg='black').place(x=0,y=511)
Label(root,width=2,height=96,bg='black').place(x=0,y=0)
Label(root,width=2,height=96,bg='black').place(x=700,y=0)
Label(root,width=2,height=96,bg='black').place(x=70,y=0)
Label(root,width=2,height=96,bg='black').place(x=140,y=0)
Label(root,width=2,height=96,bg='black').place(x=210,y=0)
Label(root,width=2,height=96,bg='black').place(x=280,y=0)
Label(root,width=2,height=96,bg='black').place(x=350,y=0)
Label(root,width=2,height=96,bg='black').place(x=420,y=0)
Label(root,width=2,height=96,bg='black').place(x=490,y=0)
Label(root,width=2,height=96,bg='black').place(x=560,y=0)
Label(root,width=2,height=96,bg='black').place(x=630,y=0)
Label(root,width=150,height=700).place(x=720,y=0)
def right():global toif to == 'south-go':c.move(head,70,0)c.move(body2, 0, 73)c.move(body1, 0, 73)to = 'south->east'elif to == 'south->east':c.move(head, 70, 0)c.move(body2, 70, 0)c.move(body1, 0, 73)to = 'east-go'elif to == 'north-go':c.move(head,70, 0)c.move(body1, 0, -73)c.move(body2, 0, -73)to = 'north->east'elif to == 'north->east':c.move(head,70, 0)c.move(body1, 0, -73)c.move(body2, 70, 0)to = 'east-go'elif to == 'east-go':c.move(body1, 70, 0)c.move(body2, 70, 0)c.move(head,70,0)
def down():global toif to == 'east-go':c.move(head,-70,73)to = 'east->south'elif to == 'east->south':c.move(head, 0, 73)c.move(body2, 0, 73)c.move(body1, 70, 0)to = 'south-go'elif to == 'west-go':c.move(head,0,73)c.move(body1,-70,0)c.move(body2, -70, 0)to ='west->south'elif to == 'west->south':c.move(head, 0, 73)c.move(body2, 0, 73)c.move(body1, -70, 0)to = 'south-go'elif to == 'south-go':c.move(head,0,73)c.move(body1, 0, 73)c.move(body2, 0, 73)
def left():global toif to == 'south-go':c.move(head, -70, 0)c.move(body2, 0, 73)c.move(body1, 0, 73)to = 'south->west'elif to == 'south->west':c.move(head, -70, 0)c.move(body2, -70, 0)c.move(body1, 0, 73)to = 'west-go'elif to == 'north-go':c.move(head,-70, 0)c.move(body1, 0, -73)c.move(body2, 0, -73)to = 'north->west'elif to == 'north->west':c.move(head,-70, 0)c.move(body1, 0, -73)c.move(body2, -70, 0)to = 'west-go'elif to == 'west-go':c.move(head, -70, 0)c.move(body2, -70, 0)c.move(body1, -70, 0)
def up():global toif to == 'west-go':c.move(head,0,-73)c.move(body1, -70, 0)c.move(body2, -70, 0)to = 'west->north'elif to == 'west->north':c.move(head,0, -73)c.move(body1, -70, 0)c.move(body2, 0, -73)to = 'north-go'elif to == 'east-go':c.move(head,0, -73)c.move(body1, 70, 0)c.move(body2, 70, 0)to = 'east->north'elif to == 'east->north':c.move(head,0, -73)c.move(body1, 70, 0)c.move(body2, 0, -73)to = 'north-go'elif to == 'north-go':c.move(head, 0, -73)c.move(body1, 0, -73)c.move(body2, 0, -73)
Button(root,text='→',width=3,command=right).place(x=769,y=37)
Button(root,text='↓',width=3,command=down).place(x=769,y=67)
Button(root,text='←',width=3,command=left).place(x=769,y=97)
Button(root,text='↑',width=3,command=up).place(x=769,y=127)
root.mainloop()

现在只是能移动,还没有别的功能

定义了4种执行状态和四种转换状态,通过按窗口上的按键进行移动

要是有伙伴可以帮我改进一下,请把代码发在评论区

谢谢!

彩蛋:抽奖机

import random
import _pyinstaller_hooks_contrib
from tkinter import *
import tkinter.messagebox
root = Tk()
root.geometry('400x200')
root.title('MY WINDOW')
def what1():tkinter.messagebox.showinfo(title='Hi', message=' 大奖 ')Label(root,text='  ',height=200,width=400).place(x=0,y=0)b = Button(root, text='     ?     ', command=what1)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what2)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what2)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what3)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what3)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what3)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what4)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what4)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what4)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=what4)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=wu)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=wu)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=wu)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=wu)b.place(x=random.randint(0, 350), y=random.randint(0, 150))b = Button(root, text='     ?     ', command=wu)b.place(x=random.randint(0, 350), y=random.randint(0, 150))
def what2():tkinter.messagebox.showwarning(title='Hi', message=' 二等奖 ')
def what3():tkinter.messagebox.askyesno(title='Hi', message=' 三等奖 ')
def what4():tkinter.messagebox.askquestion(title='Hi', message=' 四等奖 ')
def wu():tkinter.messagebox.showerror(title='Hi', message=' 无 ')
b = Button(root,text='     ?     ',command=what1)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what2)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what2)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what3)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what3)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what3)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what4)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what4)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what4)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=what4)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=wu)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=wu)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=wu)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=wu)
b.place(x=random.randint(0,350),y=random.randint(0,150))
b = Button(root,text='     ?     ',command=wu)
b.place(x=random.randint(0,350),y=random.randint(0,150))
root.mainloop()

有大奖,二,三,四等奖(当然也有没中奖)

抽到大奖后会进行下一局

四.点赞收藏,下篇博客再见

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com