-3

I'm trying to learn more about Python functions and I can't get past this road block

I'm trying to use two different functions, where each one adds a number to a file when the button gets clicked and then the final function takes in this number and reports the sum. But I keep getting a missing required positional arguments error, I'm just starting with Python and I would appreciate any help.

def printB1(event, bill1):
    f = open('myfile.txt', 'a')
    f.write('$10.99\n')
    bill1 += 10.99
    return bill1

def printB2(event, bill2):
    f = open('myfile.txt', 'a')
    f.write('$3.99\n')
    bill2 += 3.99
    return bill1


def printB6(event,  bill1, bill2):
    sumTotal = (bill1 + bill2)
    f = open('myfile.txt', 'a')
    f.write(sumTotal)

b1 = Button(topFrame, text="B1", foreground="red")
b1.bind("<Button-1>", printB1)
5
  • 3
    Why do you have two functions defined under the same name? Commented Mar 27, 2016 at 21:16
  • you should fix your indentation and duplication first Commented Mar 27, 2016 at 21:16
  • This seems related with tkinter ? Commented Mar 27, 2016 at 21:16
  • 1
    You should close your files when you're finished writing to them too Commented Mar 27, 2016 at 21:19
  • Sorry for the mistake, I fixed the function names. Commented Mar 28, 2016 at 0:34

2 Answers 2

2

The last line of your code binds the left mouse button to call printB1 when clicking b1 (which you should do with ...foreground="red", command=printB1), by the way). When you click a Button widget in Tkinter, that calls the function you've bound to it. It doesn't add any arguments on its own (this is not true everywhere in Tkinter, e.g. clicking a Canvas object passes an event automatically). Since the specified function requires two arguments, and you didn't pass any, it gives you the error you saw. If that function expects an event and a bill1... where are they? Where do you expect them to come from? You will need to either add appropriate arguments to the command with a wrapper function, or remove the parameters from printB1.

Sign up to request clarification or add additional context in comments.

3 Comments

All I'm trying to do is simply keep track of the money so that if B1 is pressed 2 times, I can calculate the sum at the end. I understand I can take the bill1 and bill2 out of the function parameters, but then I can pass the values to the final bill sum. Is there better logic to do this? As I understand the implementation is wrong
@user37649 - See here for the common way to work with data in a Tkinter app.
That is exactly what I was looking for TigerhawkT3. I'm sorry to have upset users here with a simple question, I read the article and found out that using a class with a simple if/else structure would be best suited for my problem.
1

Judging from the code, it appears you're using the tkinter GUI framework.

The specific problem you're facing is in the bind call - the function callback you've specified takes exactly two arguments (namely event and bill1), but bind only passes one argument to it (which is event). This is because bind only passes the event that's fired when you interact with the object to the function printB1. This is by design: if you want to access the value of attributes, as I suspect you are doing, there are other ways to go about doing this. See this example for better clarity, as well as a description of the event object's methods.

In other words, scrap the bill1 arguments because bind is never going to pass that value. You're going to have to figure out how to rewrite the code to do what you want - as it is, there are too many issues in the code here to address.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.