0

I have 2 .py files in the same project, one called namer and the other called GuiApp

In namer is

def nameit(name):
    greetings = f'Hello {name}'
    print(greetings)

GuiApp holds:

from tkinter import *
from namer import nameit
from functools import partial

window = Tk()
window.geometry("300x300")
txt = Entry(window, width=20)
txt.grid(row=4, column=3)
l1 = Label(window, text="Output", font=15)
l1.grid(row=6, column=0)
Btn = Button(window, text="Run X", fg="black", bg="gray", command=(nameit(txt.get())))
Btn.grid(row=4, column=2)

window.mainloop()

I don't understand why when i type something into txt, it is not returned as part of the nameit function. Any ideas please as im new to Tkinter?

  • Change `command=(nameit(txt.get()))` to `command=lambda: nameit(txt.get())` – TheLizzard Apr 28 '21 at 20:36
  • This code should not work due to indentation errors in the namer file. Please make sure your code is properly indented, or at least when you post it to StackOverflow. – Xiddoc Apr 28 '21 at 20:38
  • Does this answer your question? [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) – TheLizzard Apr 28 '21 at 21:07

1 Answers1

1

When the command of a button has parenthesis, it makes it call the function. So, create an anonymous function like: lambda: nameit(txt.get())

Solution:

from tkinter import *
from namer import nameit
from functools import partial

window = Tk()
window.geometry("300x300")
txt = Entry(window, width=20)
txt.grid(row=4, column=3)
l1 = Label(window, text="Output", font=15)
l1.grid(row=6, column=0)
Btn = Button(window, text="Run X", fg="black", bg="gray", command=lambda: nameit(txt.get()))
Btn.grid(row=4, column=2)

window.mainloop()
Tkinter Lover
  • 835
  • 3
  • 19
  • This looks a lot like my comment. – TheLizzard Apr 28 '21 at 20:44
  • My comment is 4 min older than your answer and this isn't the first time this happens today. Either you coping my comments and posting them as your answers (for reputation?) or ... – TheLizzard Apr 28 '21 at 20:50
  • I think it is the way you answered, it is not allowing me to mark yours as the answer Lizzard – Vadim Nemkov Apr 28 '21 at 20:51
  • @VadimNemkov Well for short fixes I just write comments but the problem is when people just copy/paste my answer and claim it as theirs. Most of the time they just do it for reputation. – TheLizzard Apr 28 '21 at 20:55
  • @TheLizzard I wanted to make a full answer, not comment. I don't copy comment for reputation. I just wrote my answer. – Tkinter Lover Apr 28 '21 at 20:55
  • 1
    @TheLizzard: you shouldn't be upset of someone creates an answer that looks like one of your comments. If you're not willing to make a proper answer, then there's nothing wrong with someone else doing so. – Bryan Oakley Apr 28 '21 at 21:31
  • @BryanOakley That is just reputation hunting. I don't think it is ethical. Also if someone posts an answer that is similar to mine but in a short time span I would just forget about it. But this is the second time today that TkinterLover has turned my comment into their answer. This might just be a coincidence but I really doubt it. – TheLizzard Apr 28 '21 at 21:35
  • I don't care about reputation, I care about quality answer! – Tkinter Lover Apr 28 '21 at 21:36
  • But if don't want me "copying" can't you just make your own answer? Also, you didn't specify why the OP should be using lambda's. I did. – Tkinter Lover Apr 28 '21 at 21:37
  • @TkinterLover I always post what I think is the correct answer as a comment and if/when they confirm it has worked I ask them if they want me to write a proper answer. Please note that I only do this for simple solutions like this one. If I have to define a function or something that takes more work I write a proper answer. – TheLizzard Apr 28 '21 at 21:39
  • 1
    @TheLizzard: _" I always post what I think is the correct answer as a comment and if/when they confirm it has worked"_ - that's not what comments were designed for. See [When _shouldn't_ I comment?](https://stackoverflow.com/help/privileges/comment). I'm guilty of this too, but it's a bad habit. When I do, and someone copies my comment to make an answer, I'm actually pleased rather than disappointed. I don't always have time to write a good answer, so I'm glad when someone can get inspiration and write a good answer so I don't have to. – Bryan Oakley Apr 28 '21 at 22:32