Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

python - Getting and calculating stuff through tkinter widets

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

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

1 Reply

0 votes
by (71.8m points)

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))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...