[Tkinter] Using Tkinter to calculate while formula - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Using Tkinter to calculate while formula (/thread-38103.html) |
Using Tkinter to calculate while formula - marlonsmachado - Sep-03-2022 Hello all, I'm trying to create a simple GUI interface with tkinter using a simple strokes calculator based on an user input. My simple code is below, but I cannot discover how to plot this on the tkinter interface. Could you guys help me on that? from time import sleep a = int(input('What is the total Drillstring SPM:')) stroke_rate = a # strokes/min stroke_sec = (round((stroke_rate / 60), 2)) stroke_count = 0 run = True while run == True: stroke_count += stroke_sec print(str(stroke_count) +' STROKES') sleep(1)And this was what I tried, but did not work: from tkinter import * from time import sleep root = Tk() root.title("Strokes Counter") root.resizable(False, False) def click_button(): stroke_rate = ed1.get() stroke_count = 0 run = True while run == True: stroke_count += stroke_rate sleep(1) lb2 = Label(root, print(str(stroke_count) +' stk')) lb2.grid(row=3, column=0) lb1 = Label(root, text="What is the SPM: ") lb1.grid(row=0, column=0) ed1 = Entry(root) ed1.grid(row=0, column=1) bt1 = Button(root, text="Confirm", command=click_button) bt1.grid(row=2, column=0) root.geometry("300x100") root.mainloop()Thanks in advance, guys! RE: Using Tkinter to calculate while formula - deanhystad - Sep-03-2022 Tkinter does not process events unless it is running imainloop(). You are blocking mainloop when you do this: while run == True: stroke_count += stroke_rate sleep(1)If you want your tkinter program to do something every second you should look at .after() And this is bad: lb2 = Label(root, print(str(stroke_count) +' stk')) lb2.grid(row=3, column=0)Update the label text. Don't make another label. RE: Using Tkinter to calculate while formula - Yoriz - Sep-03-2022 see Namespace flooding with * imports Here are the beginnings of turning your code into GUI code. import tkinter as tk class MainFrame(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.after_id = None self.stroke_count = 0 self.stroke_sec = 0 label = tk.Label(self, text="What is the SPM: ") label.grid(row=0, column=0) self.stroke_rate_entry = tk.Entry(self) self.stroke_rate_entry.grid(row=0, column=1) self.button = tk.Button(self, text="Start", command=self.on_btn) self.button.grid(row=2, column=0) self.stroke_amount_label = tk.Label(self, text="0 stk") self.stroke_amount_label.grid(row=3, column=0) def on_btn(self): if self.after_id: self.after_cancel(self.after_id) self.after_id = None self.button.config(text="Start") self.stroke_rate_entry.config(state="normal") self.stroke_count = 0 return stroke_rate = int(self.stroke_rate_entry.get()) self.stroke_sec = round((stroke_rate / 60), 2) self.button.config(text="Stop") self.stroke_rate_entry.config(state="disabled") self.update_stroke() def update_stroke(self): self.stroke_count += self.stroke_sec self.stroke_amount_label.config(text=f"{self.stroke_count} stk") self.after_id = self.after(1000, self.update_stroke) def main(): app = tk.Tk() app.title("Strokes Counter") app.resizable(False, False) app.geometry("300x100") main_frame = MainFrame(app) main_frame.pack() app.mainloop() if __name__ == "__main__": main() RE: Using Tkinter to calculate while formula - marlonsmachado - Sep-03-2022 Yoriz, I would like to thank you for your help. Really helped me! Thank you again! |