Getting and calculating stuff through tkinter widets

2024/10/9 12:35:51

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):passdef 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 Trueif __name__ == '__main__':convert=TimeConverterUI()
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))

https://en.xdnf.cn/q/118584.html

Related Q&A

Why does this condition execute to false when it should execute to true?

I have this code in my spider basic.py file:if l.add_xpath(price, //*[@id="price"]/text(),MapCompose(lambda i: i.replace(,, ), float),re = [,.0-9]):l.add_value(available, 1) else:l.add_value(…

Convert nested JSON to CSV in Python 2.7

Have seen a lot of thread but unable to found the solution for mine. I want to convert one nested JSON to CSV in Python 2.7. The sample JSON file is as below:sample.json # My JSON file that mainly cont…

How do I rectify this error: newline is invalid keyword argument for this function

Im currently working with raspberry pi and using DHT11 to read temperature and humidity values every second. I have to save these values into a database in real time. Heres my code that showing sensor …

How to remove substring from a string in python?

How can I remove the all lowercase letters before and after "Johnson" in these strings? str1 = aBcdJohnsonzZz str2 = asdVJohnsonkkkExpected results are as below:str1 = BJohnsonZ str2 = VJohn…

Try to print frame * and diagonal in python

I try to print * in frame and in diagonal .This is what I did:x=10 y=10 def print_frame(n, m, c):print c * mfor i in range(1, n - 1):print c , *(n-2-i),c, *i , c , cprint c * mprint_frame(10, 10, *)T…

How do I have an object rebound off the canvas border?

I am using the canvas widget from tkinter to create an ellipse and have it move around in the canvas. However when the ellipse comes in contact with the border it gets stuck to wall instead of bouncing…

How to scrape data using next button with ellipsis using Scrapy

I need to continuously get the data on next button <1 2 3 ... 5> but theres no provided href link in the source also theres also elipsis. any idea please? heres my codedef start_requests(self):u…

Execution Code Tracking - How to know which code has been executed in project?

Let say that I have open source project from which I would like to borrow some functionality. Can I get some sort of report generated during execution and/or interaction of this project? Report should…

Python code to ignore errors

I have a code that stops running each time there is an error. Is there a way to add a code to the script which will ignore all errors and keep running the script until completion?Below is the code:imp…

How to match background color of an image with background color of Pygame? [duplicate]

This question already has an answer here:How to convert the background color of image to match the color of Pygame window?(1 answer)Closed 3 years ago.I need to Make a class that draws the character a…