How to count the number of clicks for button
How to count the number of clicks for CAT or DOG button in my program and save to log.txt
from Tkinter import * import Tkinter as tk import sys stdoutOrigin=sys.stdout sys.stdout = open("log.txt", "w") master = Tk() Label(master, text='Who is your favourate animal ?? ').grid(row=0) Button(master, text='CAT' ,).grid(row=1, sticky=W, pady=4) Button(master, text='DOG' ,).grid(row=1,column=1,sticky=W, pady=4) mainloop() sys.stdout.close() sys.stdout=stdoutOrigin
I don't know if this script is the best one to overwrite a number in a file, but you can try it. If your file is empty, it will create lines (for ex : dog = 0
), and if it exists it will increment it (for ex : dog = 1
) when you click on the button.
I also remove your from Tkinter import *
, and instead I replaced Button
by tk.Button
for all widgets.
def save_in_file(animal): f = open("log.txt", "r+") animal_exists = False data = f.read() # separate the file into lines lines = data.split("\n") # list of lines : ['dog = 2', 'cat = 1'] for i, v in enumerate(lines): # separate the lines into words words = v.split() # list of words : ['dog', '=', '3'] if animal in words: animal_exists = True # here we search for the "number_to_increment" number_to_increment = words[-1] # we convert "number_to_increment" into integer, then add 1 new_number = str(int(number_to_increment) +1) # we convert "new_number" back to string words[-1] = new_number # concatenate words to form the new line lines[i] = " ".join(words) # creates a new line with "animal = 0" if "animal" is not in file if not animal_exists: if lines[0] == "": lines.remove("") lines.append("{} = 0".format(animal)) # concatenate all lines to get the whole text for new file data = "\n".join(lines) f.close() # open file with write permission f = open("log.txt", "wt") # overwrite the file with our modified data f.write(data) f.close() def cat(): save_in_file("cat") def dog(): save_in_file("dog") import tkinter as tk master = tk.Tk() tk.Label(master, text='Who is your favourate animal ?? ').grid(row=0) tk.Button(master, text='CAT', command=cat).grid(row=1, sticky="w", pady=4) tk.Button(master, text='DOG', command=dog).grid(row=1,column=1,sticky="w", pady=4) master.mainloop()
Output :
# log.txt dog = 2 cat = 1
How to count the number of times a button is clicked?, I tried to write a script that increments a variable value, every time a button is clicked. <script type="text/javascript"> // if the count variable is� I have a page where i want to count the number of clicks on a button. and the numbers are shown just below that button. I tried to search and found this. I think this will not count the total number of clicks: Keeping track of number of button clicks. I am familiar with javascript code, so any help would be useful.
cats = 0 dogs = 0 def cat(): with open("log.txt").read() as contents: file = open("log.txt", "w") file.write(str(int(contents.split()[0]) + 1) + "\n" + contents.split()[1]) file.close() def dog(): with open("log.txt").read() as contents: file = open("log.txt", "w") file.write(contents.split()[0] + "\n" + str(int(contents.split()[1]) + 1)) file.close() Button(master, text='CAT' , command=cat).grid(row=1, sticky=W, pady=4) Button(master, text='DOG' , command=dog).grid(row=1,column=1,sticky=W, pady=4)
Store a counter permanently, localStorage.clickcount = Number(localStorage.clickcount)+1; innerHTML = " You have clicked the button " + localStorage.clickcount + Close the browser tab (or window), and try again, and the counter will continue to count (is not reset). � � I tried to write a script that increments a variable value, every time a button is clicked. <script type="text/javascript"> // if the count variable is undefined, set its value to zero if
You can also just make a command that adds one to a variable every click.
Button Counter, Tired of manually counting or pressing a specific key to know how many times you have clicked that button in your work? Look no further� If I right-click the button in Design Mode and select Properties, I get sheet properties not the button properties. I can't seem to locate the command button properties any longer. I still see the button name "cmdButtonGetInfo" and "=EMBED("Forms.CommandButton.1","") in the name box and formula bar.
Click Count with JavaScript, We are counting how many times the first button is hovered. Hovering over RESET button will set the count to it's initial state. 1. 2. 3. If you want to know how many links they've clicked on in the current page, the answer is zero since if they click on a link they should go to another page. If you are using a listener to prevent navigation, then the same listener can also count clicks.
Click Counter with JavaScript, var button = document.getElementById("clickme"),. 2. count = 0;. 3. button.onclick = function() {. 4. count += 1;. 5. button.innerHTML = "Click me: " + count;. 6. }; 7. At the moment the user can press the button as many times as they want. I dont actually know how to count or monitor the amount of times a button in tkinter has been pressed. I would be very grateful if someone could provide me with some code for Python(3.1.4) that I could use to count the amount of times the button has been pressed.
Option Compare Database Option Explicit Public ClickCount As Integer '=========================================== Private Sub cmdClickButton_Click () ClickCount = Me.cmdClickButton.Caption + 1 Me.cmdClickButton.Caption = ClickCount End Sub. This will increase the caption by one number.
Comments
- Thank you so much Dear requirement was same as u code. But I did not understand that what you did in def save_in_file(animal): Can you explain line by line if possible?
- I updated my answer with comments, tell me if it's still not clear
- Hi, I am getting below error, please clarify Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\naman\AppData\Local\Programs\Python\Python38-32\lib\tkinter_init_.py", line 1883, in call return self.func(*args) File "C:/Users/naman/OneDrive/Desktop/tst or.py", line 13, in cat with open("log.txt").read() as contents: AttributeError: enter
- You need to make a log.txt file containing two zeros on separate lines before you start
with open("log.txt").read() as contents:
will raise exception becausestr
does not supportcontext manager
feature.- Your answer is quite short you should explain it before you start getting downvotes.
- Please elaborate
- sure, you need you button to do something on click so you give it a command check this out [link] stackoverflow.com/questions/6874525/… and see how they do there buttons