Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was wondering how to calculate stuff using tkinter buttons. I'm making a simple program to calculate seconds to hours:minutes:seconds. The user inputs an integer using the entry widget on the seconds box and when they press calculate, they get the result via the converted time line. I'm confused on how to start calculating it. I know you get the integer via .get, but I'm stuck on how to do that and calculate it in a h:m:s format. This is my code so far.

import tkinter
from tkinter import *




class TimeConverterUI():


    def __init__(self):

        self.root_window = Tk()
        self.root_window.geometry('400x150')
        self.root_window.title('Seconds Converter')
        self.text()
        self.calculate_button()
        self.quit_button()
        self.root_window.wait_window()


    def text(self):

        row_label = tkinter.Label(
              master = self.root_window, text = 'Seconds: ')

        row_label.grid( row = 0, column = 0, columnspan=2, padx=10, pady=10,

                           sticky = tkinter.W)

        secondsEntry = Entry(master = self.root_window)
        secondsEntry.grid(row = 0, column = 1)

        row_label = tkinter.Label(
              master = self.root_window, text = 'Converted Time(H:M:S): ').grid(row=1)

    def calculate_button(self):

        quit = Button(self.root_window, text = "Calculate", command = self.calculate)
        quit.grid(row = 3, column = 0, columnspan = 3, pady=20,
                  sticky = tkinter.W)

    def calculate(self):

        pass

    def quit_button(self):

        quit = Button(self.root_window, text = "Quit", command = self.quit)
        quit.grid(row = 3, column = 3, columnspan = 3, pady=20,
                  sticky = tkinter.E)

    def quit(self) -> bool:

        self.root_window.destroy()
        return True





if __name__ == '__main__':

    convert=TimeConverterUI()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
228 views
Welcome To Ask or Share your Answers For Others

1 Answer

First break this code below into 2 lines if you ever want to use row_label later because this will return NoneType. You should define it first then use .grid on it (just like your button).

row_label = tkinter.Label(
              master = self.root_window, text = 'Converted Time(H:M:S): ').grid(row=1)

Now you can create another label to show the result. Remember to put self. before its name so you can use it in the calculate function. Also change secondsEntry to self.secondsEntry for the same reason.
Now you just use int(self.secondsEntry.get()) in that function and do the required calculations.
Then set the result to that result label with .configure(text=str(result))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...