[转载] 如何使用Python 实现秒表功能?
参考链接: 在Python中使用Tkinter创建秒表
其实python不是我们看到那么复杂,如果打好扎实的基础,我们可以用python做一些好玩的事情,比如实现秒表功能,一起来看下把~ 前言: 本文的重点是在python中使用Tkinter创建秒表。 关于Tkinter: Tkinter是Python的标准GUI库。Python与Tkinter结合使用时,提供了一种创建GUI应用程序的快速而简单的方法。Tkinter为Tk GUI工具包提供了一个强大的面向对象接口。Tkinter很容易入门,下面是一些示例代码,可以让您在python中使用Tkinter。 语法: # Python program to create a # a new window using Tkinter # importing the required libraires import tkinter
# creating a object ‘top’ as instance of class Tk top = tkinter.Tk()
# This will start the blank window top.mainloop() 输出:
使用Tkinter创建秒表 现在让我们尝试使用Tkinter模块创建一个程序来创建秒表。 所需模块:我们将仅使用tkinter来创建gui,并且此程序中将不使用其他任何库。 # Python program to illustrate a stop watch # using Tkinter
```java
#importing the required libraries
import tkinter as Tkinter
counter = -1
running = False
def counter_label(label):
def count():
if running:
global counter
# To manage the intial delay.
if counter==-1:
display="Starting..."
else:
display=str(counter)
label['text']=display # Or label.config(text=display)
# label.after(arg1, arg2) delays by
# first argument given in milliseconds
# and then calls the function given as second argument.
# Generally like here we need to call the
# function in which it is present repeatedly.
# Delays by 1000ms=1 seconds and call count again.
label.after(1000, count)
counter += 1
# Triggering the start of the counter.
count()
# start function of the stopwatch
def Start(label):
global running
running=True
counter_label(label)
start['state']='disabled'
stop['state']='normal'
reset['state']='normal'
# Stop function of the stopwatch
def Stop():
global running
start['state']='normal'
stop['state']='disabled'
reset['state']='normal'
running = False
# Reset function of the stopwatch
def Reset(label):
global counter
counter=-1
# If rest is pressed after pressing stop.
if running==False:
reset['state']='disabled'
label['text']='Welcome!'
# If reset is pressed while the stopwatch is running.
else:
label['text']='Starting...'
root = Tkinter.Tk()
root.title("Stopwatch")
# Fixing the window size.
root.minsize(width=250, height=70)
label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold")
label.pack()
start = Tkinter.Button(root, text='Start',
width=15, command=lambda:Start(label))
stop = Tkinter.Button(root, text='Stop',
width=15, state='disabled', command=Stop)
reset = Tkinter.Button(root, text='Reset',
width=15, state='disabled', command=lambda:Reset(label))
start.pack()
stop.pack()
reset.pack()
root.mainloop()
输出:
好了,以上就是使用Python 实现秒表功能的全部内容了,如需了解更多python实用知识,点击进入PyThon学习网教学中心。
[转载] 如何使用Python 实现秒表功能?相关推荐
- python秒表游戏_如何使用Python 实现秒表功能?
其实python不是我们看到那么复杂,如果打好扎实的基础,我们可以用python做一些好玩的事情,比如实现秒表功能,一起来看下吧~ 前言: 本文的重点是在python中使用Tkinter创建秒表. 关 ...
- python秒表模块_如何使用Python 实现秒表功能?
其实python不是我们看到那么复杂,如果打好扎实的基础,我们可以用python做一些好玩的事情,比如实现秒表功能,一起来看下吧~ 前言: 本文的重点是在python中使用Tkinter创建秒表. 关 ...
- Python 实现秒表功能
以下实例使用 time 模块来实现秒表功能: 实例: import timeprint('按下回车开始计时,按下 Ctrl + C 停止计时.') while True:try:input() # 如 ...
- [转载] python字典查询功能_Python中的字典功能
参考链接: Python中的字典dictionary方法 (cmp(), len(), items()-) python字典查询功能 Let's check out some important fu ...
- python解释器的功能_python的解释器是什么?
python解释器是解释python脚本执行的程序.编写python代码保存后,我们会得到一个以.py为扩展名的文本文件.要运行此文件,就需要python解释器去执行.py文件. python解释器种 ...
- python网络爬虫的基本步骤-详解Python网络爬虫功能的基本写法
网络爬虫,即Web Spider,是一个很形象的名字.把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛. 1. 网络爬虫的定义 网络蜘蛛是通过网页的链接地址来寻找网页的.从网站某一个 ...
- pycharm支持python3.8_PyCharm 2019.2 发布,Python 3.8功能的支持
PyCharm 2019.2已经发布了,令人兴奋的新功能,开始支持Python 3.8,改进的Jupyter Notebook体验以及更多其他更改. PyCharm 2019.2发布亮点: 改进了Ju ...
- python开发环境功能介绍_第一模块 第3章 Python介绍与环境配置
python入门(全为重点) 1. 编程语言介绍 编程语言分类.总结 2. python介绍 3. 解释器多版本共存 4. 运行python程序的两种方式 5. 一个python程序运行的三个步骤(* ...
- python实现登录功能_python实现用户登录功能模块
python实现登录功能模块#!/usr/bin/env python while True: user = raw_input('Please input username:') if user = ...
最新文章
- 「mysql优化专题」90%程序员面试都用得上的索引优化手册(5)【面试重点】
- 【Laravel 之 Cache 模块】 使用database drive
- javascript 与vbscript 互相调用
- 引擎: 决定数据库存取数据的方式 = 不同的特点 = 不同的用户体验 数据类型: 规定了数据库可以存放哪些数据 约束: 限制存储数据的规则 键...
- iText in Action 2nd5.4节(Adding page events to PdfWriter)读书笔记
- dataframe 选择输出_使用 Python 实现机器学习特征选择的 4 种方法
- The credentials you provided during SQL Server 2008 install are invalid
- [译]Vulkan教程(20)重建交换链
- java camel swagger,Swagger将下划线转换为camelcase
- 图片适应窗口_毕业论文排版保姆级教程——图片和公式排版
- 【转载】规则化和模型选择(Regularization and model selection)
- Affinity Publisher for Mac(实用桌面排版工具)
- 单片机定时器一1ms12MHz_单片机原理及接口技术笔记1单片机概述
- javacv 写mp4_JavaCV 实现视频转码
- 魔兽和星际最大差别是什么?
- html实现凹陷效果,css3怎么实现字体凹陷凸出效果?(附代码)
- win10浏览器闪退_Win10系统下Edge浏览器经常闪退的解决方法
- 查询时报错The error may involve defaultParameterMap ### The error occurred while setting parameters
- 关于 OpenSSL“心脏出血”漏洞的分析
- 病毒不可怕,就怕流氓有文化