0

I am writing a program that requires two values (from 2 entries) to be called when a button is pressed. I simplified to code to try to isolate the problem. For some reason the program is not behaving how I want it to. When the button is pressed, the output is "A=" and then if i click the button a second time I get ""A=entry1 B=entry2 A=". I have been trying to figure out the problem for hours now, please help.

import tkinter as tk

def button_function():    
    A = entry1.get()
    print('A=', A)
    
    B= entry2.get()
    print('B=', B)
   
root = tk.Tk()

canvas = tk.Canvas(root)
canvas.pack()

entry1 = tk.Entry(root)
entry1.place(relwidth=0.5, relheight=0.5)

entry2 = tk.Entry(root)
entry2.place(rely=0.5, relwidth=0.5, relheight=0.5)

button = tk.Button(root, text = "confirm", command= button_function)
button.place(relx=0.5, relwidth=0.5, relheight=1)

root.mainloop()
  • 2
    Change `command=button_function()` to `command=button_function`. – acw1668 Jul 16 '21 at 11:35
  • [Does this help?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) –  Jul 16 '21 at 11:37
  • Why have you edited the code and added the right part? This doesn't make sense – PCM Jul 16 '21 at 12:52
  • @PCM with right part also he is not getting expected result :( he has mentioned it in question! I am wondering how is this possible! – imxitiz Jul 16 '21 at 12:59

1 Answers1

2

You just need to change command=button_function() to command=button_function, then it will work perfectly!

imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • @Charles mark this answer as accepted, if this solves your problem! – imxitiz Jul 16 '21 at 12:15
  • I tried this, it unfortunately does not seem like it made a difference. – Charles Goldman Jul 16 '21 at 12:15
  • What is the issue, now you are facing? Because, this solved for me. I had tried and answer your question! – imxitiz Jul 16 '21 at 12:17
  • I added lambda in also so I can click the button more than once. What I find quite strange is if for example if I input entry1 as (3) and entry2 as (5) and then click the button, the output is "A=", then if I click the button a second time the output is "A= 3 B= 5 A=" and if i click the button a third time the output is "A= 3 B= 5 A=3 B= 5 A=" – Charles Goldman Jul 16 '21 at 12:19
  • Had I wrote lambda in my answer. No, you don't have to use `lambda` to make working for multiple times! – imxitiz Jul 16 '21 at 12:21
  • I really appreciate your assistance, sorry I am very new to Tkinter, the code for my actual program is about 200 lines so I just simplified it to isolate the issue. To my knowledge, if lambda is not included then the button can only be clicked once, is this not right? I have tried the code with and without "lambda" and the problem still persists. I just included it to be able try look for a pattern with the output that might assist for finding the cause to the problem. – Charles Goldman Jul 16 '21 at 12:31
  • I edited the code above to what it currently looks like, and the output is behaving very strangely – Charles Goldman Jul 16 '21 at 12:32
  • As of my knowledge, I don't really think without using `lambda` you can't click button multiple times. And I have enough experience to say you that. and what is the weird output you are getting? – imxitiz Jul 16 '21 at 12:39
  • For example, if I input entry1 as (3) and entry2 as (5) and then click the button, the output should be "A=3 B=5" but instead the output is "A=", then if I click the button a second time the output is "A= 3 B= 5 A=" and if i click the button a third time the output is "A= 3 B= 5 A=3 B= 5 A=" – Charles Goldman Jul 16 '21 at 12:44
  • Please edit and share your exact code (200 lines) by mentioning me, need to see. Otherwise someone may down vote or report you question for sharing unnecessary. So, try to edit, it I wanna see your full code. – imxitiz Jul 16 '21 at 12:48
  • The program is for something else entirely, the portion that I posted above is where the issue lies so there is no need to post the rest of it, it will be hard to make much sense of what is going on. The exact issue I am describing lies with the exact code I haveposted above – Charles Goldman Jul 16 '21 at 12:52
  • If you are sure about that, then in your given code I didn't found any wrong thing that will make your issue. It worked for me exactly as I expected. Try to check typos and indentations. – imxitiz Jul 16 '21 at 12:54
  • That is very odd, I have copied and pasted the code, I just did it again to make sure and I am still getting the exact same output. – Charles Goldman Jul 16 '21 at 12:56
  • Make new file and copy paste your given code. And check and inform me. What is the output? **Don't change anything, just copy and paste from here and run it.** – imxitiz Jul 16 '21 at 12:57
  • It is still "A=", then if I click the button a second time the output is "A= 3 B= 5 A=" and if i click the button a third time the output is "A= 3 B= 5 A=3 B= 5 A=" – Charles Goldman Jul 16 '21 at 12:59
  • You have created new file, and just do copy/paste and you are still getting the output you are getting then, I don't really think I have any idea about that! Because while doing the same for me it worked. I had also try doing the same thing like you told writing `3` in entry1 and `5` in entry2 and output is `A= 3/nB= 5` not `/n` but you understand right? Which is exactly what I had expected! – imxitiz Jul 16 '21 at 13:06
  • That is very strange, thank you so much for trying though. I will try the code out on a different IDE and pc when I get home later, not sure how it will make a difference but I can't think of anything else that could be the cause. – Charles Goldman Jul 16 '21 at 13:12
  • Yeah! Try doing that and inform me. And try checking some other things also that could make this in your **complete code**! – imxitiz Jul 16 '21 at 13:14
  • @CharlesGoldman have you checked it? Is the code working, if it is working the mark this as answer! – imxitiz Jul 17 '21 at 01:52