Jul-18-2018, 10:11 AM
(This post was last modified: Jul-18-2018, 10:11 AM by robertofreemano.)
Hi,
I'm a Python noob, trying to learn by creating a project (which is how I learn best)... I'm struggling to enable/disable these buttons (see code snippet.
Actions:
I have searched the web for a solution but failing to understand why it's not working. The below code I found example online and trying to add.
I understand it like: "[Button name].configure(state="disabled")" but I've interpreted the code START button as sw.Start.configure(state="disabled")
Any help would be appreciated - In the meantime, I will continue to try fix this.
Roberto
I'm a Python noob, trying to learn by creating a project (which is how I learn best)... I'm struggling to enable/disable these buttons (see code snippet.
Actions:
- STOP button is disabled
- click START
- disable START button / enable STOP
I have searched the web for a solution but failing to understand why it's not working. The below code I found example online and trying to add.
I understand it like: "[Button name].configure(state="disabled")" but I've interpreted the code START button as sw.Start.configure(state="disabled")
Any help would be appreciated - In the meantime, I will continue to try fix this.
try:
# for Python2
from Tkinter import * ## notice capitalized T in Tkinter
except ImportError:
# for Python3
from tkinter import * ## notice lowercase 't' in tkinter here
import time
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
""" Make the time label. """
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
#ME------------------------------------------------------------------
Button.sw.Start.configure(state="disabled")
#--------------------------------------------------------------------
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
# save file
with open("C:\pytest\stopwatch_test.txt","a") as f:
f.write(self.timestr.get())
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
def main():
root = Tk()
sw = StopWatch(root)
sw.pack(side=TOP)
Button(root, text='Start', command=sw.Start).pack(side=LEFT)
Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
Button(root, text='Quit', command=root.quit).pack(side=LEFT)
root.mainloop()
if __name__ == '__main__':
main()Thanks,Roberto
