1

I am trying to write a Program, in which u are able to open a seperate testing/debug window. Like, a second window including some buttons to affect the main window. I already tried a few things and i am able to open two seperate windows but whatever i am trying to draw is drawn in the main window.

import tkinter as tk
from tkinter import *

def debugWindow():
    dbWin = tk.Tk()
    dbWin.title("Debug")
    btn = tk.Button(text="Test")
    btn.pack()
    dbWin.mainloop()

window = tk.Tk()
window.title("Mainwindow")
btn2 = tk.Button(text="Start debug Window", command=debugWindow)
btn2.pack()
window.mainloop()

So that's what i tried, but as i said the second button renders in the first window. Also i am pretty new to Python so if this is not the way u normally do it, please correct me. I am still learning :) And also sorry for my english, i am not a native speaker.

2
  • You want to use Toplevel() for that second window - calling Tk() a second time does give you a second window, but it lives in an entirely separate GUI environment that cannot interact with the original environment. To put widgets in a specific window (or other container), pass it as the first parameter to the widget. Commented May 13, 2021 at 21:26
  • @Zuckerpapa As @ jsonharper said change btn = tk.Button(text="Test") to btn = tk.Button(dbWin, text="Test") to tell tkinter that btn's master should be the second window (otherwise it assumes that the first window is the master). Commented May 13, 2021 at 21:30

1 Answer 1

2

If you want to make the button stay on the window called dbWin ,then, instead of using:

btn = tk.Button(text="Test")

which python assumes that the button is suppose to go on the main window, use:

btn = tk.Button(dbWin, text="Test")

Using this will specifically assign the second parameter thus allowing your button to go on the window dbWin

(credit to those who said this before me in the comments)

Sign up to request clarification or add additional context in comments.

5 Comments

Isn't this what @jasonharper and I said in the comments?
@TheLizzard: if you want credit for an answer, you need to write an answer, not a comment. Don't fault someone else for creating an answer that has the same information as a comment. In this specific case, you don't know if they got the answer from your comment or if they simply knew the answer and submitted it.
@BryanOakley Given that it is 8 min after jasonharper posted the comment, it isn't right to take full credit for the answer. It is possible that it took DavidLiu 8 min to work out the problem and type the answer but I don't think that is likely. I think that giving credit is a must. It is fine if you disagree with me but I will still try to give credit where it is due
@TheLizzard: There's nothing wrong with someone writing a correct answer that happens to coincide with your comment. If you want credit, write an answer rather than a comment. Yes, they should give credit, but you shouldn't automatically assume they copied your comment. You're accusing them without any evidence.
@BryanOakley I don't think it is a coincidence given that it 8 min and 25 sec after jasonharper wrote that comment. I decided that writing my own answer wouldn't be respectful to jasonharper as he found the problem in OP's code first. There is no way of getting evidence so I am using probabilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.