-5

I'm trying to make it so every time I click the button I can run something different.

Counter = 0

def B_C(Counter):
    Counter = Counter + 1
    if counter == 1:
        print("1")
    elif counter == 2:
        print("2")
    else:
         if counter == 3:
             Print("3")

But I get

TypeError: B_C() takes exactly 1 positional argument (0 given)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jayde
  • 11
  • 1
  • 3

2 Answers2

4

try this:

counter = 0

def B_C():
    global counter
    counter += 1
    if counter == 1:
        print("1")
    elif counter == 2:
        print("2")
    else:
         if counter == 3:
             print("3")

B_C()
B_C()
B_C()

Output:

1
2
3

First thing: python is case sensitive, so Counter is not equal to counter. And in the function you can use global counter so you don't need to pass counter to button click.

hexerei software
  • 3,100
  • 2
  • 15
  • 19
  • using globals defiantly is not the preferred way. for local use, testing, quick tasks, no problem to use, but if you write a real application for deployment you should avoid globals and place your counter in an own class as Justin has suggested with his `MyCounter()` class – hexerei software Apr 01 '15 at 13:34
2

Don't use globals ... If you want to modify an object python has mutable objects. These are structures that are passed by reference and the function will change the value inside the structure everywhere.

Below is a basic example of pass by value where the value outside the scope of the function does not change. Below the variable "c" is an immutable object, the value of c will not be changed from a function. Immutable vs Mutable types

c = 0
def foo(c):
    c = c + 1
    return c

m = foo(c)
print(c) # 0
print(m) # 1

Here is an example of a mutable object and pass by reference (I believe python always does pass by reference but has mutable and immutable objects).

c = [0]
def foo(c):
    c[0] = c[0] + 1
    return c

m = foo(c)
print(c) # [1]
print(m) # [1]

or make a class. Anything but globals.

class MyCount(object):
    def __init__(self):
        self.x = 0
    # end Constructor

    def B_C(self):
        self.x += 1
        pass # do stuff
    # end B_C

    def __str__(self):
        return str(self.x)
    # end str
# end class MyCount

c = MyCount()
c.B_C()
print(c)
c.B_C()
print(c)

Also you mentioned that you were using a button. If you want a button press to pass an argument into the function you may have to use a lambda function. I don't really know about TKinter, but for PySide you have connect the button to call a function on click. There may not be an easy way to pass a variable into the button click function. http://www.tutorialspoint.com/python/tk_button.htm

def helloCallBack(txt):
    tkMessageBox.showinfo("Hello Python", txt)

# from the link above
B = Tkinter.Button(top, text="Hello", command= lambda x="Hello": helloCallBack(x))
# lambda is actually a function definition.
# The lambda is like helloCallBack without the parentheses.
# This helps you pass a variable into a function without much code
Community
  • 1
  • 1
justengel
  • 6,132
  • 4
  • 26
  • 42
  • i would also suggest your `class MyCount()` sample for production use, but for quick and dirty, non-threading and local use globals are fine i reckon :) – hexerei software Apr 01 '15 at 13:36