Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding 1 to number
#1
I cant find anwser anywhere for this question. I have a number and when i click a button i want that number + 1 and so on so like this. 

from tkinter import *
root = Tk()
zeroo = 0
def add():
    zeroo = 0
    for zeroo in range(900):
        zeroo = zeroo + 1
       print(zeroo)
x = Button(root,text="Do it!", command=add)
x.pack()
x.mainloop()
But this code only prints 900 and what I want is this
When you click the button it prints 1
When you click again it prints 2
and so on but i dont know how
Reply
#2
I don't know Tkinter at all but try this

from tkinter import *
root = Tk()
zeroo = 0
def add(zeroo):
    if zeroo <= 900:
        return zero + 1
x = Button(root,text="Do it!", command=add)
y = Label(root, text=zeroo)
x.pack()
x.mainloop()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Well that just prints nothing

#2 well i edited the code
no error but it prints nothing

from tkinter import *
root = Tk()
zeroo = 0
def add():
    if zeroo <= 900:
        return zeroo + 1
        print(zeroo)
x = Button(root,text="Do it!", command=add)
y = Label(root, text=zeroo)
x.pack()
x.mainloop()
Reply
#4
You can use IntVar() which has a get method.
>>> import tkinter
>>> root = tkinter.Tk()
>>> counter = tkinter.IntVar()
>>> counter.get() + 1
1
import tkinter

root = tkinter.Tk()
root.geometry("200x100")

counter = tkinter.IntVar()
def add():
    counter.set(counter.get() + 1)

tkinter.Label(root, textvariable=counter).pack()
tkinter.Button(root, text="Do it!", command=add, fg="dark green").pack()
root.mainloop()
Do no use from tkinter import * even if this is common in many Tkinter tutorials.
Reply
#5
(Apr-29-2017, 01:14 PM)JohnNo Wrote: #2 well i edited the code
no error but it prints nothing

       return zeroo + 1
        print(zeroo)

Of coarse it prints nothing, you return from the function before ever trying to print anything.

Aside from that, though, try to avoid global variables.  It might be a hassle at first, but it'll make your life much better later on :)

All the cool kids have posted code, so here's some from me, too:
>>> import tkinter as tk
>>> class Counter(object):
...   def __init__(self, start: int = 0):
...     self.value = start
...   def add(self) -> int:
...     if self.value < 900:
...       self.value += 1
...     print(self.value)
...     return self.value
...
>>> clicks = Counter()
>>> root = tk.Tk()
>>> tk.Button(root, text="click me!", command=clicks.add).pack()
>>> root.mainloop()
1
2
3
4
5
6
>>>
Reply
#6
You get 900 because you made the script go in a range from 0 to 899 with x
Remove that ****.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding an ascending number [SOLVED] AlphaInc 3 2,282 Jul-11-2021, 10:13 AM
Last Post: perfringo
  adding elements to a list that are more than a specific number Olavv 2 2,236 Mar-19-2020, 06:05 PM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 3,001 Mar-08-2020, 11:16 AM
Last Post: ndc85430
  Adding markers to Folium map only adding last element. tantony 0 2,160 Oct-16-2019, 03:28 PM
Last Post: tantony
  naming images adding to number within multiple functions Bmart6969 0 1,943 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Adding a line number to an lxml Element vindy 0 3,402 Mar-08-2019, 08:34 PM
Last Post: vindy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020