-1

I had started writing a tkinter program when I stumbled across this problem in my code:

 elif (xtimes=="" or xtimes=="Optional") and (t!="" or t!="Optional"):
    amount
    years=0
    while years<t:
        principle=principle+((interest/100)*(principle))
        years+=1

    amount=principle
    finallabel=Label(root,text="Your Final Amount will be",amount,"at the end of",years,"years")
    finallabel.grid(row=13,column=0)

Under the elif statement, I have calculated amount and I want to show the answer using a label, which gives the error: "positional argument follows keyword argument"

I think that I want to send the variable amount through the text, like in normal python, but the code makes it think like I am passing some parameter called amount which doesn't exist.

Please help.

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54

1 Answers1

1

the only positional argument you need to pass is Label(root). So if you do Label(text='my text', root) it gives this error.

this works:

import tkinter as tk
root = tk.Tk()

lab = tk.Label(root, text='hi')
lab.pack()
root.mainloop()

this not:

import tkinter as tk
root = tk.Tk()

lab = tk.Label(text='hi',root)
lab.pack()
root.mainloop()

After update.. Let's look on this line of your code here:

finallabel=Label(root,text="Your Final Amount will be",amount,"at the end of",years,"years")

What you did here, was to parse arguments through the interface of the Label class to make an instance of it, with the config of given arguments.

the tkinter Label class knows the arguments that can be found here.

So comparing your Label with the available parameters, you will notice that amount and years arent part of them. The only Posistional argument that the Label class of tkinter is expecting is the master, followed by keyword arguments **options. Read this.

What you trying to do is a string with variables and there are several ways to achieve this. My personal favorite is the f'string. And with a f'string your code will be look like this:

finallabel=Label(root,text=f'Your Final Amount will be {amount},at the end of {years} years')

Let me know if something isnt clear.

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • This statement makes no sense: _"the only keyword argument you need to pass is Label(root)"_ - `root` is not a keyword argument. Did you mean to write "the only **positional** argument..."? – Bryan Oakley Jul 26 '20 at 13:53
  • i have posted the code...i need to show the answer using label, but iam getting the error. pls help –  Jul 26 '20 at 14:02