Use start and stop function with same button in Tkinter

2024/10/6 20:38:23

With the help of the command button, I am able to disconnect the frame in Tkinter. But is there any way which helps to use the same button to start also?

import tkinter as tk
counter = 0
def counter_label(label):def count():global countercounter+=1label.config(text=counter)label.after(1000, count)count()root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()

Suggestions will be grateful

Answer

You could simple use an if/else statement to check if the buttons text is Start or Stop then change a Boolean variable that controls the counter while also update the text on the button.

import tkinter as tkcounter = 0
active_counter = Falsedef count():if active_counter:global countercounter += 1label.config(text=counter)label.after(1000, count)def start_stop():global active_counterif button['text'] == 'Start':active_counter = Truecount()button.config(text="Stop")else:active_counter = Falsebutton.config(text="Start")root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
button = tk.Button(root, text='Start', width=25, command=start_stop)
button.pack()
root.mainloop()

Here is an OOP example as well:

import tkinter as tkclass App(tk.Tk):def __init__(self):super().__init__()self.title("Counting Seconds")self.counter = 0self.active_counter = Falseself.label = tk.Label(self, fg="green")self.label.pack()self.button = tk.Button(self, text='Start', width=25, command=self.start_stop)self.button.pack()def count(self):if self.active_counter:self.counter += 1self.label.config(text=self.counter)self.label.after(1000, self.count)def start_stop(self):if self.button['text'] == 'Start':self.active_counter = Trueself.count()self.button.config(text="Stop")else:self.active_counter = Falseself.button.config(text="Start")if __name__ == "__main__":App().mainloop()
https://en.xdnf.cn/q/118908.html

Related Q&A

How to restrict my students to dont access teacher area in django? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

invert edge values in python boolean list

I have a list of booleans likel = [False, False, False, True, True, True, False, False, True, False, False]and want to invert every edge value that is False like[True, True, True, True, True, True, Fal…

Unable to write text on mouseclick area on Image

I am trying to draw text on Image where the user clicks. Getting this error:Exception in Tkinter callback Traceback (most recent call last):File "C:\Users\Admin\AppData\Local\Programs\Python\Pytho…

Google Cloud Storage: __init__() got an unexpected keyword argument total_size

I am developping a tool to transcribe interviews for a contract I have. For that I develop a code with the following flow:After input validation, the audio file (in m4a) is converted to wav and stored …

Selenium, Intercept HTTP Request?

Using selenium 4.12 in Python, how can I intercept an HTTP request to see what its body or headers look like? Please Note, that Im not asking for code but rather for resources/ideas of different or su…

Flask server returns 404 on localhost:5000 w/ Twilio

Im following this guide (Python Quickstart: Replying to SMS and MMS Messages) to try and set up a flask server, but when I try to connect to http://localhost:5000 I get a 404 error. I can ping 127.0.0.…

printing values and keys from a dictionary in a specific format (python)

I have this dictionary (name and grade):d1 = {a: 1, b: 2, c: 3}and I have to print it like this:|a | 1 | C | |b | 2 | B | |c | 3 | …

stdscr.getstr() ignore keys, just string

I just need convert entered text(bytes) to string. But if i on cyrillic press Backspace and some character, python throw me this error:UnicodeDecodeError: utf-8 codec cant decode byte 0xd0 in position …

What is wrong with the following program code, attempting to initialize a 4 x 4 matrix of integers?

What is wrong with the following program code, attempting to initialize a 4 x 4 matrix of integers? How should the initialization be done?line = [0] * 4 matrix = [line, line, line, line]

Creating a Data Pipeline to BigQuery Using Cloud Functions and Cloud Scheduler

I am trying to build a Data Pipeline that will download the data from this website and push it to a BigQuery Table. def OH_Data_Pipeline(trigger=Yes):if trigger==Yes:import pandas as pdimport pandas_gb…