https://www.lawebdelprogramador.com/codigo/Python/4752-Simple-cronometro-con-TK.html
from tkinter import Tk,Label,Button,Frame
proceso=0
def iniciar(contador=0):
global proceso
time['text'] = contador
proceso=time.after(1000, iniciar, (contador+1))
def parar():
global proceso
time.after_cancel(proceso)
root = Tk()
root.title('Cronometro')
time = Label(root, fg='red', width=20, font=("","18"))
time.pack()
frame=Frame(root)
btnIniciar=Button(frame, fg='blue', text='Iniciar', command=iniciar)
btnIniciar.grid(row=1, column=1)
btnParar=Button(frame, fg='blue', text='Parar', command=parar)
btnParar.grid(row=1, column=2)
frame.pack()
root.mainloop()
Saludos,
- Tienes que leer los errores que muestra Python:
C:\Users\EdSon\Desktop>tk_contador.py
File "C:\Users\EdSon\Desktop\tk_contador.py", line 20
time.after_cancel(proceso)
^
IndentationError: unexpected indent
- Dice que hay una Indentación no esperada en la línea 20, luego revisas tu código y te das cuenta de que efectivamente tienes:
def parar():
global proceso
time.after_cancel(proceso)
root = Tk()
En lugar de:
def parar():
global proceso
time.after_cancel(proceso)
root = Tk()
Funciona lo que me dices, pero ahora me sale error mas abajo debe ser otra cosa.
Traceback (most recent call last):
File "C:\Users\xiqui\Desktop\python\xiqui2\cronometro.py", line 40, in <module>
btnParar=Button(frame, fg='blue', text='Parar', command=parar)
NameError: name 'parar' is not defined
https://likegeeks.com/es/ejemplos-de-la-gui-de-python/
from tkinter import Tk,Label,Button,Frame
proceso=0
def iniciar(contador=0):
global proceso
time['text'] = contador
proceso=time.after(1000, iniciar, (contador+1))
def parar():
global proceso
time.after_cancel(proceso)
root = Tk()
root.title('Cronometro')
time = Label(root, fg='red', width=20, font=("","18"))
time.pack()
frame=Frame(root)
btnIniciar=Button(frame, fg='blue', text='Iniciar', command=iniciar)
btnIniciar.grid(row=1, column=1)
btnParar=Button(frame, fg='blue', text='Parar', command=parar)
btnParar.grid(row=1, column=2)
frame.pack()
root.mainloop()
A mi este código me funciona. Debería ser igual. Escribe todo tu código de esta manera, donde dice aquí y quita los espacios.
[ code=python ] Aqui [ /code ]
Cita de: Guarrino en 11 Febrero 2019, 12:19 PM
from tkinter import Tk,Label,Button,Frame
proceso=0
def iniciar(contador=0):
global proceso
time['text'] = contador
proceso=time.after(1000, iniciar, (contador+1))
def parar():
global proceso
time.after_cancel(proceso)
root = Tk()
root.title('Cronometro')
time = Label(root, fg='red', width=20, font=("","18"))
time.pack()
frame=Frame(root)
btnIniciar=Button(frame, fg='blue', text='Iniciar', command=iniciar)
btnIniciar.grid(row=1, column=1)
btnParar=Button(frame, fg='blue', text='Parar', command=parar)
btnParar.grid(row=1, column=2)
frame.pack()
root.mainloop()
A mi este código me funciona. Debería ser igual. Escribe todo tu código de esta manera, donde dice aquí y quita los espacios.
[ code=python ] Aqui [ /code ]
Lo probe quitando los espacios i me saldo error en la linia 3, me estado peleando un rato con la composicon i no hay manera
- Quizá sea un problema de Indentación, quita toda las indentaciones y vuélvelas a poner, todas deben de ser exactamente iguales en espacios o tabulaciones no vale mezclar.