基于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()