Home > Mobile >  How to get the status of button in tkinter?
How to get the status of button in tkinter?

Time:12-23

from tkinter import *
import time

def checkTime():
    if len(hourInput.get()) != 0 and len(minuteInput.get()) != 0 and len(secondInput.get()) != 0:
        if hourInput.get() == time.strftime("%H"):
            print("good")
    
    window.after(500, checkTime)

def pressButton(button):
        button.config(relief=SUNKEN)

if __name__=='__main__':

    window = Tk()

    window.geometry("1920x1080")

    canvas = Canvas(window, width = 1980, height = 1020)
    
    canvas.pack()

    hourInput = StringVar()
    minuteInput = StringVar()
    secondInput = StringVar()

    setHour = Entry(window, text = hourInput, font = (20))
    setHour.place(x = 100, y = 20, width = 100, height = 40)

    setMinute = Entry(window, text = minuteInput, font = (20))
    setMinute.place(x = 300, y = 20, width = 100, height = 40)


    setSecond = Entry(window, text = secondInput, font = (20))
    setSecond.place(x = 500, y = 20, width = 100, height = 40)
    
    canvas.create_text(60, 40, text = "Hour: ", font = (20))

    canvas.create_text(260, 40, text = "Minute: ", font = (20))

    canvas.create_text(460, 40, text = "Second: ", font = (20))

    submit = Button(text = "Submit", height = 2, width = 10, font = (10))
    submit.config(command = lambda submit=submit:pressButton(submit))

    submit.place(x = 100, y = 100)
    
    checkTime()

    window.mainloop()   

I want the function checkTime() to be called when my button is pressed. But how to get the status of my button and compare it ? I want to use the function only if the button is pressed as a test that the user agree with his inputs

CodePudding user response:

You can modify the button declaration as follows so that the checkTime() will trigger when the button is pressed.

submit = Button(text = "Submit", height = 2, width = 10, font = (10), relief=SUNKEN)
submit['command'] = checkTime  # no parentheses here

Also make sure that the checkTime() method call in the bottom is removed

CodePudding user response:

I put the function checkTime() inside the pressButton() function, and now the program works fine.

from tkinter import *
import time

def checkTime():
    if len(hourInput.get()) != 0 and len(minuteInput.get()) != 0 and len(secondInput.get()) != 0:
        if hourInput.get() == time.strftime("%H"):
            print("good")
    window.after(500, checkTime)

def pressButton(button):
        button.config(relief = SUNKEN)
        checkTime()

if __name__== '__main__':

    window = Tk()

    window.geometry("1920x1080")

    canvas = Canvas(window, width = 1980, height = 1020)
    
    canvas.pack()

    hourInput = StringVar()
    minuteInput = StringVar()
    secondInput = StringVar()

    setHour = Entry(window, text = hourInput, font = (20))
    setHour.place(x = 100, y = 20, width = 100, height = 40)

    setMinute = Entry(window, text = minuteInput, font = (20))
    setMinute.place(x = 300, y = 20, width = 100, height = 40)


    setSecond = Entry(window, text = secondInput, font = (20))
    setSecond.place(x = 500, y = 20, width = 100, height = 40)
    
    canvas.create_text(60, 40, text = "Hour: ", font = (20))

    canvas.create_text(260, 40, text = "Minute: ", font = (20))

    canvas.create_text(460, 40, text = "Second: ", font = (20))

    submit = Button(text = "Submit", height = 2, width = 10, font = (10))
    submit.config(command = lambda submit=submit:pressButton(submit))

    submit.place(x = 100, y = 100)

    window.mainloop()
    

  • Related