Python Tkinter小程序

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

一.时钟

python 复制代码
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行:刷新

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

二.猜数游戏

python 复制代码
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行:刷新

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

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

我还会改进!

python 复制代码
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 to
    if 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 to
    if 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 to
    if 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 to
    if 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种执行状态和四种转换状态,通过按窗口上的按键进行移动

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

谢谢!

彩蛋:抽奖机

python 复制代码
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()

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

抽到大奖后会进行下一局

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

相关推荐
迷迭所归处9 分钟前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ38 分钟前
Java 23 的12 个新特性!!
java·开发语言·学习
FreakStudio38 分钟前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
leon62539 分钟前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
锦亦之22332 小时前
QT+OSG+OSG-earth如何在窗口显示一个地球
开发语言·qt
我是苏苏2 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
姜太公钓鲸2332 小时前
c++ static(详解)
开发语言·c++
菜菜想进步2 小时前
内存管理(C++版)
c语言·开发语言·c++
2301_789985942 小时前
Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
java·开发语言·学习
知星小度S2 小时前
C语言——自定义类型
c语言·开发语言