基于python中Tkinter的计数器

基于python中Tkinter的计数器

from tkinter import *
import tkinter as tk
from tkinter.ttk import Separator

#config
counter=0
def run_counter(digit):
    def counting():
        global counter
        digit.config(text=str(counter))
        digit.after(1000,counting) 
        counter+=1
    counting()    
#复位
def counter_0():
    global counter 
    counter=0
#记录
def out_counter():
    global counter
    print('现在计数到了'+str(counter))          

root=tk.Tk()
root.title('hello')

'''居中显示'''
width=300
height=300
#获取屏幕尺寸
scree_width=root.winfo_screenwidth()
scree_height=root.winfo_screenheight()
size_geo='%dx%d+%d+%d'%(width,height,(scree_width-width)/2,(scree_height-height)/2)
root.geometry(size_geo)
# 可以自由拉伸大小
root.resizable(1, 1)
# 设置窗口一直置顶
root.attributes('-topmost', 1)
# 设置透明度
root.attributes('-alpha', 0.9)

#image1=tk.PhotoImage(file='001.gif')
text1=tk.Label(root,text='计数器',font=('宋体',20,'bold'))
text1.pack(padx=10,pady=10)

sep=Separator(root,orient=tk.HORIZONTAL)
sep.pack(fill=X,padx=5)

digit=tk.Label(root,font=('宋体',20,'bold'))
digit.pack()

sep=Separator(root,orient=tk.HORIZONTAL)
sep.pack(fill=X,padx=5)

button1=tk.Button(root,text='开始',command=lambda:run_counter(digit),bg='#e7bf22')
button1.pack(padx=100,pady=10,fill=X)

button1=tk.Button(root,text='复位',command=counter_0,bg='#e7bf22')
button1.pack(padx=100,pady=10,fill=X)

button1=tk.Button(root,text='计数',command=out_counter,bg='#e7bf22')
button1.pack(padx=100,pady=10,fill=X)

button1=tk.Button(root,text='退出',command=root.quit,bg='#e7bf22')
button1.pack(padx=100,pady=10,fill=X)

#run_counter(digit)
root.mainloop()

运行界面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值