Python Forum
[Tkinter] disable/enable button | stopwatch proj
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] disable/enable button | stopwatch proj
#1
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:
  • 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
Reply
#2
see: http://infohost.nmt.edu/tcc/help/pubs/tk...utton.html
use tk.disabled()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo PySimpleGUI FilesBrowse enable event to update value in combo SamLiu 2 4,592 Mar-15-2023, 12:49 PM
Last Post: SamLiu
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 5,525 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  [Tkinter] binding versus disable DPaul 6 6,737 May-05-2021, 05:17 PM
Last Post: DPaul
  How to disable custom button Sancho_Pansa 7 3,492 Dec-04-2020, 02:21 PM
Last Post: buran
  How to disable focus on Frame in Tkinter? szafranji 1 3,019 May-13-2020, 10:45 PM
Last Post: DT2000
  Disable entry field and still see value scratchmyhead 5 5,112 May-11-2020, 08:09 PM
Last Post: menator01
  stopwatch amschueller 5 2,487 May-04-2020, 04:21 AM
Last Post: ndc85430
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 2,664 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,012 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Disable Enter Key PyQt5 Wizard maffaz 1 6,709 Jul-02-2018, 09:45 AM
Last Post: maffaz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020