0

the problem is:

return self.func(*args)
TypeError: b1_pg() missing 1 required positional argument: 'e1'

i search entire google but i didn't found any thing

from tkinter import *
from tkinter import ttk
from functools import partial

root = Tk()
root.geometry('400x200+100+200')
def b1_pg(e1):
    search = str(e1.get())
    print(search)
    return search

what = ttk.Label(root, text="who do you want to search about???").grid(row=1, column=0)

e1 = ttk.Entry(root, width=40).grid(row=1, column=1)

b1 = ttk.Button(root, text="if you are ready prees here", command=b1_pg).grid(row=2, column=0)

#information=ttk.Label(root,text=family{search})

root.title('family')
root.mainloop()

i expect the code take the value from e1(entry) when i preesd the button but it give me an error

return self.func(*args)
TypeError: b1_pg() missing 1 required positional argument: 'e1'

what's the wrong in my code?

Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • Check there is enough elements in the `args` variable before you try [unpacking](https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists) it – liamdiprose Oct 03 '19 at 21:53
  • The function called by a button's `command=` option is not passed any parameters. – jasonharper Oct 04 '19 at 00:57
  • `el.get()` will always return none due to how you use `grid()`. You have to do `el.grid()` on a new line in order to use `get()` on `el`. – Mike - SMT Oct 04 '19 at 13:42

1 Answers1

0

You have a few things we need to correct here.

One issue that you use grid() on the same line you are defining the Entry field. Because of this e1 is actually going to always return None due to grid() being assigned in this way.

To fix this you can do e1.grid() on a new line and this will allow you to use e1.get() without issues.

That said lets correct your imports as * is bad in the sense you will end up overwriting methods.

So instead of doing:

from tkinter import *

Do this:

import tkinter as tk

We should also change a few things in your function.

The return section is not going to be doing anything of use here. You cannot return values to the button that called the function. It cannot be used in any way so you can remove that line. If you need to use that value somewhere then you can send it from the function to wherever you need it.

Related to your error in you question. There is no need for the argument e1 in your function. You are not passing an argument to the function in the first place so this will cause an error. Second you are already calling e1.get() from the global namespace so there is no need for an argument.

Lastly you do not need to do str(e1.get()). The get() method is already returning a string. It will always return a string.

See below code and let me know if you have any questions:

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.title('family')
root.geometry('400x200+100+200')


def b1_pg():
    search = e1.get()
    print(search)


ttk.Label(root, text="who do you want to search about???").grid(row=1, column=0)
e1 = ttk.Entry(root, width=40)
e1.grid(row=1, column=1)
ttk.Button(root, text="if you are ready press here", command=b1_pg).grid(row=2, column=0)

root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79