0

I just started working on python and trying to build a small user interface. I am having multiple similar windows with a button on it and a callback function associated with the button. Is there a way to know from which window the button was pressed. I need to know from which window "Create message" was pressed.x argument would be the window number from the button is pressed

for x in range(num):
    Label(newWindow, text="Enter TTL").grid(row=1, column=0)
            TTL = Entry(newWindow, width=10, borderwidth=5)
            TTL.grid(row=1, column=1)
            #ttl = int(TTL.get())
            Label(newWindow, text="Enter Direction").grid(row=2, column=0)
            direction = Ent`enter code here`ry(newWindow, width=10, borderwidth=5)
            direction.grid(row=2, column=1)
            #dir = int(direction.get())
            Button(newWindow, text="Create Message",
            command=lambda: threading.Thread(target=create_message, args=(TTL, direction,x,)).start()).grid(row=4,
                                                                                                                column=0)

enter image description here

  • This is one reason why Tkinter is such crap. You CAN do this by capturing variables in a lambda when you assign the callback, but it's ugly. If you are really interested in doing Python GUIs, please take the time to learn one of the real frameworks, like Qt/PySide or wxPython. – Tim Roberts Jul 02 '21 at 04:49
  • Thanks for the response Tim, I just started working on Python, I will try to explore other options later. – Abhinav Wadhwa Jul 02 '21 at 04:51
  • It will be easier for us to help you if you post a minimal example. – Art Jul 02 '21 at 05:02
  • You can make two separate windows like this - `window1 = Tk()` and `window2 = Tk()`. So that you will know in which window the button was clicked. But having multiple instances of `Tk()` is a problem – PCM Jul 02 '21 at 05:14
  • Your code does not create multiple windows. It only creates widgets inside same window `newWindow`. Also fix the indentation of your code. – acw1668 Jul 02 '21 at 05:56
  • Also your code has the same issue as the question: [tkinter-assign-button-command-in-loop-with-lambda](https://stackoverflow.com/questions/17677649/tkinter-assign-button-command-in-loop-with-lambda). – acw1668 Jul 02 '21 at 06:03
  • ACW1668 - Thanks, can you please elaborate how these 2 issues are same. – Abhinav Wadhwa Jul 02 '21 at 06:05
  • The answer of the linked question has already explain the cause of the issue. – acw1668 Jul 02 '21 at 06:15

0 Answers0