How do I make this type of spinbox for a schedule?

Viewed 52

enter image description here

I would like to do like this I am trying another code but they come out 2 times there is no way to add it the same as that this is code that I got in another question

import tkinter as tk

class App(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.hourstr=tk.StringVar(self,'10')
        self.hour = tk.Spinbox(self,from_=0, to=23, wrap=True, textvariable=self.hourstr, width=2,state="readonly")
        self.minstr=tk.StringVar(self,'30')
        self.minstr.trace("w", self.trace_var)
        self.last_value = ""
        self.min = tk.Spinbox(self, from_=0, to=59, wrap=True, textvariable=self.minstr, width=2, state="readonly")
        self.hour.grid()
        self.min.grid(row=0,column=1)

    def trace_var(self,*args):
        if self.last_value == "59" and self.minstr.get() == "0":
            self.hourstr.set(int(self.hourstr.get())+1 if self.hourstr.get() !="23" else 0)
        self.last_value = self.minstr.get()

root = tk.Tk()
App(root).pack()
root.mainloop()
1 Answers

here it is, an example of what You wanted (similar to the photo):

from tkinter import Tk, Spinbox


def time_values():
    times = []
    postfix = 'AM'
    for minute in range(0, 1440):
        if minute >= 720:
            postfix = 'PM'
        hour = minute // 60
        minutes = minute - (hour * 60)
        if hour < 10:
            hour = f'0{hour}'
        if minutes < 10:
            minutes = f'0{minutes}'
        times.append(f'{hour}:{minutes} {postfix}')
    return times


root = Tk()

spinbox = Spinbox(root, values=time_values(), state='readonly', wrap=True, repeatinterval=10)
spinbox.pack()

root.mainloop()
Related