Jump to content
def start_time():
        while scoreboard.clockState:
            time.sleep(0.0755)
            if scoreboard.tenths<=0:
                scoreboard.seconds-=1
                scoreboard.tenths=9
                if scoreboard.seconds<0:
                    scoreboard.minutes-=1
                    scoreboard.seconds=59
            else:
                scoreboard.tenths-=1
            if scoreboard.seconds==0 and scoreboard.clockState and scoreboard.minutes==0 and scoreboard.tenths==0:
                scoreboard.minutes=0
                scoreboard.seconds=0
                minuteLabel.set_value('00')
                secondLabel.set_value('0')
                mainminuteLabel.set_value('00')
                mainsecondLabel.set_value('0')
                clockLabel.config(text='00.0  ')
                scoreboard.update()
                scoreboard.clockState=False
                if scoreboard.autoHorn:
                    buzzer()
                break
             
            if (scoreboard.minutes>0 or scoreboard.seconds>shotclock.seconds) and shotclock.enabled:
                 if shotclock.clockState and shotclock.seconds>=0:
                    if shotclock.seconds<=0:
                        shotclock.seconds=0
                        shotclock.tenths=0
                        shotTimeLabel.set_value('00')
                        if scoreboard.autoHorn:
                                horn()
                        shotclock.clockState=False
                        runButton.state(['!selected'])
                        if scoreboard.autoStopClock:
                            scoreboard.clockState=False
                            break
                    else:
                        shotclock.tenths-=1
                        if shotclock.tenths<0:
                            shotclock.seconds-=1
                            shotclock.tenths=9
                    if shotclock.seconds<10:
                        shotTimeLabel.set_value('0'+str(shotclock.seconds))
                        shotLabel.config(text='0'+str(shotclock.seconds))
                    else:
                        shotTimeLabel.set_value(str(shotclock.seconds))
                        shotLabel.config(text=str(shotclock.seconds))
            else:
                    shotTimeLabel.set_value('')
                    shotLabel.config(text='    ')
                    shotclock.seconds=0
                    shotclock.tenths=0
                    channel.stop()
                    shotclock.clockState=False
                    runButton.state(['!selected'])
            if scoreboard.seconds<10 and scoreboard.minutes>0:
                if scoreboard.minutes<10:
                    minuteLabel.set_value(str(scoreboard.minutes))
                    secondLabel.set_value('0'+str(scoreboard.seconds))
                    clockLabel.config(text='  '+str(scoreboard.minutes)+':0'+str(scoreboard.seconds))
                    mainminuteLabel.set_value(str(scoreboard.minutes))
                    mainsecondLabel.set_value('0'+str(scoreboard.seconds))
                else:
                    minuteLabel.set_value(str(scoreboard.minutes))
                    secondLabel.set_value('0'+str(scoreboard.seconds))
                    clockLabel.config(text=str(scoreboard.minutes)+':0'+str(scoreboard.seconds))
                    mainminuteLabel.set_value(str(scoreboard.minutes))
                    mainsecondLabel.set_value('0'+str(scoreboard.seconds))
            elif scoreboard.minutes==0:
                if scoreboard.seconds<10:
                    minuteLabel.set_value('0'+str(scoreboard.seconds))
                    secondLabel.set_value(str(scoreboard.tenths))
                    clockLabel.config(text='0'+str(scoreboard.seconds)+'.'+str(scoreboard.tenths)+'  ')
                    mainminuteLabel.set_value('0'+str(scoreboard.seconds))
                    mainsecondLabel.set_value(str(scoreboard.tenths))
                else:
                    minuteLabel.set_value(str(scoreboard.seconds))
                    secondLabel.set_value(str(scoreboard.tenths))
                    clockLabel.config(text=str(scoreboard.seconds)+'.'+str(scoreboard.tenths)+'  ')
                    mainminuteLabel.set_value('0'+str(scoreboard.seconds))
                    mainsecondLabel.set_value(str(scoreboard.tenths))
            else:
                if scoreboard.minutes<10:
                    minuteLabel.set_value(str(scoreboard.minutes))
                    secondLabel.set_value(str(scoreboard.seconds))
                    clockLabel.config(text='  '+str(scoreboard.minutes)+':'+str(scoreboard.seconds))
                    mainminuteLabel.set_value(str(scoreboard.minutes))
                    mainsecondLabel.set_value(str(scoreboard.seconds))
                else:
                    minuteLabel.set_value(str(scoreboard.minutes))
                    secondLabel.set_value(str(scoreboard.seconds))
                    clockLabel.config(text=str(scoreboard.minutes)+':'+str(scoreboard.seconds))
                    mainminuteLabel.set_value(str(scoreboard.minutes))
                    mainsecondLabel.set_value(str(scoreboard.seconds))
            scoreboard.update()
            controlpanel.update()
            shotclock.update()

How can I make this shorter while doing the same function? I need three variables for time, scoreboard.minutes, scoreboard.seconds, and scoreboard.tenths. This is because when the time is over a minute, the clock displays mm:ss, such as 20:15, and when the time is under a minute, it shows ss.t, such as 43.7. Here's my code for the displays:

minuteLabel=SevenSegmentDigits(scoreboard, digits=2, background='black', digit_color='yellow', height=100)
minuteLabel.grid(column=3,row=1)
timecolon=Label(scoreboard, text=':', foreground='white', background='black', font=('Century Gothic', 75))
timecolon.grid(column=4, row=1)
secondLabel=SevenSegmentDigits(scoreboard, digits=2, background='black', digit_color='yellow', height=100)
secondLabel.grid(column=5, row=1)

 

Link to comment
https://linustechtips.com/topic/1386162-how-can-i-make-this-code-shorter/
Share on other sites

Link to post
Share on other sites

TLDR '🙂

 

But given your description, you might want to always have full time mm:ss.tt stored. When called just check for mm>0.

Sorry if misunderstood op

 

Edit:

Refactor, cause attention span

Edited by rikitikitavi
Link to post
Share on other sites

Is there any reason why you are doing a sleep of 0.0755 and subtracting 1 from 10ths of a second?  In theory you are causing a drift, which generally isn't good if it's meant to be for a real world thing.

 

In general, it's not the best to keep track of time using multiple variables like that, it just adds to clutter.  Aside from using the built in timing to get more accurate numbers, if you wanted to do it using the ticking method you currently are implementing, then I would recommend just storing the time left in a single number.

 

scoreboard.timeInTenths = 3000 (this would be 5 minutes)

def start_time():
        while scoreboard.clockState:
            time.sleep(0.0755)
            scoreboard.timeInTenths -= 1
            hour = scoreboard.timeInTenths // 36000   [You now have hour]
            minute = (scoreboard.timeInTenths % 36000) // 600 [You have minutes]
            seconds = (scoreboard.timeInTenths % 600) // 10 [You have seconds]
            tenths = (scoreboard.timeInTenths % 10) [You have tenths now]
            
            if scoreboard.timeInTenths = 0: [You can use the simplified stuff here, knowing timeInTenths represents the total time remaining

 

3735928559 - Beware of the dead beef

Link to post
Share on other sites

3 hours ago, wanderingfool2 said:

Is there any reason why you are doing a sleep of 0.0755 and subtracting 1 from 10ths of a second?  In theory you are causing a drift, which generally isn't good if it's meant to be for a real world thing.

 

In general, it's not the best to keep track of time using multiple variables like that, it just adds to clutter.  Aside from using the built in timing to get more accurate numbers, if you wanted to do it using the ticking method you currently are implementing, then I would recommend just storing the time left in a single number.

 


scoreboard.timeInTenths = 3000 (this would be 5 minutes)

def start_time():
        while scoreboard.clockState:
            time.sleep(0.0755)
            scoreboard.timeInTenths -= 1
            hour = scoreboard.timeInTenths // 36000   [You now have hour]
            minute = (scoreboard.timeInTenths % 36000) // 600 [You have minutes]
            seconds = (scoreboard.timeInTenths % 600) // 10 [You have seconds]
            tenths = (scoreboard.timeInTenths % 10) [You have tenths now]
            
            if scoreboard.timeInTenths = 0: [You can use the simplified stuff here, knowing timeInTenths represents the total time remaining

 

The time is set using two Spinboxes, one for minutes, one for seconds. How can I accomplish that?'

def set_time():
        if controlpanel.settingTime==False and (not scoreboard.clockState):
            minuteEntry.config(state=NORMAL)
            secondEntry.config(state=NORMAL)
            setTimeButton.config(text='Confirm Time')
            controlpanel.settingTime=True
        else:
            try:
                setErrorLabel.config(text='')
                if int(minuteEntry.get())>99 or int(minuteEntry.get())<0 or int(secondEntry.get())>59 or int(secondEntry.get())<0:
                    setErrorLabel.config(text='Error: Value(s) are invalid.')
                elif not scoreboard.clockState:
                    scoreboard.minutes=int(minuteEntry.get())
                    scoreboard.seconds=int(secondEntry.get())
                    setTimeButton.config(text='Set Time')
                    controlpanel.settingTime=False
                    minuteEntry.config(state=DISABLED)
                    secondEntry.config(state=DISABLED)
                if scoreboard.seconds<10 and scoreboard.minutes>0:
                    if scoreboard.minutes<10:
                        minuteLabel.set_value(str(scoreboard.minutes))
                        secondLabel.set_value('0'+str(scoreboard.seconds))
                        clockLabel.config(text='  '+str(scoreboard.minutes)+':0'+str(scoreboard.seconds))
                        mainminuteLabel.set_value(str(scoreboard.minutes))
                        mainsecondLabel.set_value('0'+str(scoreboard.seconds))
                    else:
                        minuteLabel.set_value(str(scoreboard.minutes))
                        secondLabel.set_value('0'+str(scoreboard.seconds))
                        clockLabel.config(text=str(scoreboard.minutes)+':0'+str(scoreboard.seconds))
                        mainminuteLabel.set_value(str(scoreboard.minutes))
                        mainsecondLabel.set_value('0'+str(scoreboard.seconds))
                elif scoreboard.minutes==0:
                    if scoreboard.seconds<10:
                        minuteLabel.set_value('0'+str(scoreboard.seconds))
                        secondLabel.set_value(str(scoreboard.tenths))
                        clockLabel.config(text='0'+str(scoreboard.seconds)+'.'+str(scoreboard.tenths)+'  ')
                        mainminuteLabel.set_value('0'+str(scoreboard.seconds))
                        mainsecondLabel.set_value(str(scoreboard.tenths))
                    else:
                        minuteLabel.set_value(str(scoreboard.seconds))
                        secondLabel.set_value(str(scoreboard.tenths))
                        clockLabel.config(text=str(scoreboard.seconds)+'.'+str(scoreboard.tenths)+'  ')
                        mainminuteLabel.set_value('0'+str(scoreboard.seconds))
                        mainsecondLabel.set_value(str(scoreboard.tenths))
                else:
                    if scoreboard.minutes<10:
                        minuteLabel.set_value(str(scoreboard.minutes))
                        secondLabel.set_value(str(scoreboard.seconds))
                        clockLabel.config(text='  '+str(scoreboard.minutes)+':'+str(scoreboard.seconds))
                        mainminuteLabel.set_value(str(scoreboard.minutes))
                        mainsecondLabel.set_value(str(scoreboard.seconds))
                    else:
                        minuteLabel.set_value(str(scoreboard.minutes))
                        secondLabel.set_value(str(scoreboard.seconds))
                        clockLabel.config(text=str(scoreboard.minutes)+':'+str(scoreboard.seconds))
                        mainminuteLabel.set_value(str(scoreboard.minutes))
                        mainsecondLabel.set_value(str(scoreboard.seconds))
                    if (scoreboard.minutes>0 or scoreboard.seconds>shotclock.seconds) and shotclock.enabled:
                        if shotclock.seconds<10:
                            shotTimeLabel.set_value('0'+str(shotclock.seconds))
                            shotLabel.config(text='0'+str(shotclock.seconds))
                        else:
                            shotTimeLabel.set_value(str(shotclock.seconds))
                            shotLabel.config(text=str(shotclock.seconds))
                    else:
                        shotTimeLabel.set_value('')
                        shotLabel.config(text='    ')
                        shotclock.clockState=False
                        runButton.state(['!selected'])
            except ValueError:
                setErrorLabel.config(text='Error: Value(s) are invalid.')
        scoreboard.update()
        controlpanel.update()
        shotclock.update()

...

minuteEntry=Spinbox(controlpanel, from_=0, to=99, state=DISABLED)
minuteEntry.grid(column=3, row=2)
secondEntry=Spinbox(controlpanel, from_=0, to=59, state=DISABLED)
secondEntry.grid(column=3, row=3)
setTimeButton=Button(controlpanel, text='Set Time', command=set_time, state=NORMAL)
setTimeButton.grid(column=3, row=4)

 

Link to post
Share on other sites

Obvious duplicate code is :

minuteLabel.set_value(str(...))
secondLabel.set_value(str(...))
clockLabel.config(text=str(...))
mainminuteLabel.set_value(str(...))
mainsecondLabel.set_value(str(...))

That you call 6 times.

 

 

Use f-strings:

f'{scoreboard.seconds:02d}'

returns the desired formatted string

Link to post
Share on other sites

Just now, Umberto said:

Obvious duplicate code is :


minuteLabel.set_value(str(...))
secondLabel.set_value(str(...))
clockLabel.config(text=str(...))
mainminuteLabel.set_value(str(...))
mainsecondLabel.set_value(str(...))

That you call 6 times.

 

 

Use f-strings:


f'{scoreboard.seconds:02d}'

returns the desired formatted string

I can't display a colon with SevenSegmentDigits, so I need separate min and sec displays. Also, the mainminuteLabel and mainSecondLabel are for the seperate shot clock screen.

Link to post
Share on other sites

22 minutes ago, 38034580 said:

I can't display a colon with SevenSegmentDigits, so I need separate min and sec displays. Also, the mainminuteLabel and mainSecondLabel are for the seperate shot clock screen.

Look up f-strings

 

What I mean is instead of doing this:

if scoreboard.seconds<10 and scoreboard.minutes>0:
    if scoreboard.minutes<10:
        minuteLabel.set_value(str(scoreboard.minutes))
        secondLabel.set_value('0'+str(scoreboard.seconds))
        clockLabel.config(text='  '+str(scoreboard.minutes)+':0'+str(scoreboard.seconds))
        mainminuteLabel.set_value(str(scoreboard.minutes))
        mainsecondLabel.set_value('0'+str(scoreboard.seconds))
    else:
        minuteLabel.set_value(str(scoreboard.minutes))
        secondLabel.set_value('0'+str(scoreboard.seconds))
        clockLabel.config(text=str(scoreboard.minutes)+':0'+str(scoreboard.seconds))
        mainminuteLabel.set_value(str(scoreboard.minutes))
        mainsecondLabel.set_value('0'+str(scoreboard.seconds))
elif scoreboard.minutes==0:
    if scoreboard.seconds<10:
        minuteLabel.set_value('0'+str(scoreboard.seconds))
        secondLabel.set_value(str(scoreboard.tenths))
        clockLabel.config(text='0'+str(scoreboard.seconds)+'.'+str(scoreboard.tenths)+'  ')
        mainminuteLabel.set_value('0'+str(scoreboard.seconds))
        mainsecondLabel.set_value(str(scoreboard.tenths))
    else:
        minuteLabel.set_value(str(scoreboard.seconds))
        secondLabel.set_value(str(scoreboard.tenths))
        clockLabel.config(text=str(scoreboard.seconds)+'.'+str(scoreboard.tenths)+'  ')
        mainminuteLabel.set_value('0'+str(scoreboard.seconds))
        mainsecondLabel.set_value(str(scoreboard.tenths))
else:
    if scoreboard.minutes<10:
        minuteLabel.set_value(str(scoreboard.minutes))
        secondLabel.set_value(str(scoreboard.seconds))
        clockLabel.config(text='  '+str(scoreboard.minutes)+':'+str(scoreboard.seconds))
        mainminuteLabel.set_value(str(scoreboard.minutes))
        mainsecondLabel.set_value(str(scoreboard.seconds))
    else:
        minuteLabel.set_value(str(scoreboard.minutes))
        secondLabel.set_value(str(scoreboard.seconds))
        clockLabel.config(text=str(scoreboard.minutes)+':'+str(scoreboard.seconds))
        mainminuteLabel.set_value(str(scoreboard.minutes))
        mainsecondLabel.set_value(str(scoreboard.seconds))

Where each set_value method is duplicated 6 times, you can remove all of that using f-strings and calling each method once.

For minuteLabel it would look like this:

minuteLabel.set_value(f'{scoreboard.seconds:02d}' if scoreboard.minutes==0 else str(scoreboard.minutes))

 

Edit: This would reduce 39 lines of code down to just 5. 

Edited by Umberto
Link to post
Share on other sites

5 hours ago, 38034580 said:

The time is set using two Spinboxes, one for minutes, one for seconds. How can I accomplish that?'

I wrote a longer thing, but lost it in the forum update...oh well...here is a shorter version

 

Oh correction to my earlier

minutes = (timeInTenths // 600) % 60 [I had written the prior when I was too tired and not thinking]

seconds = (timeInTenths // 10) % 60

 

You can just manipulate the single variable, it makes it so much easier (because then you don't have to worry later if you do like add 61 minutes)

e.g. setting precise time from info

timeInTenths = tenths + seconds * 10 + minutes * 600 + hours * 36000

 

e.g. Adjusting to a specific hour (keeps minutes seconds intact)

timeInTenths += (hours - timeInTenths // 36000) * 36000

 

e.g. Adjusting to a specific minute (keeps hours seconds intact)

timeInTenths += (minutes - (timeInTenths // 600) % 60) * 600

 

Adding 61 minutes

timeInTenths += 61 * 600

3735928559 - Beware of the dead beef

Link to post
Share on other sites

11 hours ago, Umberto said:

Obvious duplicate code is :

minuteLabel.set_value(str(...))
secondLabel.set_value(str(...))
clockLabel.config(text=str(...))
mainminuteLabel.set_value(str(...))
mainsecondLabel.set_value(str(...))

That you call 6 times.

 

 

Use f-strings:

f'{scoreboard.seconds:02d}'

returns the desired formatted string

I need to know how to make it, using your method, to switch the time display to showing tenths, like 53.7, when under a minute and show mm:ss when over a minute, like 20:00.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×