Python Forum
[Tkinter] DUAL WINDOWS... - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] DUAL WINDOWS... (/thread-29943.html)



DUAL WINDOWS... - ATARI_LIVE - Sep-26-2020

My goal is dual windows as left and right for example:
from tkinter import *

window_left = Tk()
window_left.geometry("300x300")
window_left.configure(bg='black')
window_left.title("LEFT")

window_right = Tk()
window_right.geometry("300x300")
window_right.configure(bg='black')
window_right.title("RIGHT")

Label(window_left, text="left", fg="White")
Label(window_right, text="right", fg="White")

window_right.mainloop()
window_left.mainloop()
no print text at all.. something I missed in the code?

I looked in google. they keep talk about buttons, I do not want that.


RE: DUAL WINDOWS... - deanhystad - Sep-26-2020

Tk() creates the tkinter application and returns a window. You cannot call Tk twice because this messes up the application. Other GUI toolkits do a better job separating windows from application.

If you want multiple windows in a tkinter application you call Tk() once and then create more windows using TopLevel.

Creating multiple top level windows for an application is generally frowned upon and is considered bad design. You should consider making one window with multiple frames.


RE: DUAL WINDOWS... - DT2000 - Sep-26-2020

This is an example of what deanhystad is stating on using Toplevel() however in this example closing the main Tk() window (window_left) all windows close also.
from tkinter import *

window_left = Tk()
window_left.geometry("300x300")
window_left.configure(bg='black')
window_left.title("LEFT")

window_right = Toplevel()
window_right.geometry("300x300")
window_right.configure(bg='black')
window_right.title("RIGHT")

Label(window_left, text="left", fg="White")
Label(window_right, text="right", fg="White")

window_right.mainloop()
window_left.mainloop()



RE: DUAL WINDOWS... - ATARI_LIVE - Sep-27-2020

Umm.. Thank for that but my point is the text did not print on both.
I am using Raspberry Pi 4B 4GB
I changed the text see here.
from tkinter import *
 
window_left = Tk()
window_left.geometry("300x300")
window_left.configure(bg='black')
window_left.title("LEFT")
 
window_right = Toplevel()
window_right.geometry("300x300")
window_right.configure(bg='black')
window_right.title("RIGHT")
 
Label(window_left, text="HELLO WORLD FROM LEFT", font=("Droid Sans Fallback", 30), fg="White", bg="black")
Label(window_right, text="HELLO WORLD FROM RIGHT", font=("Droid Sans Fallback", 30), fg="White", bg="black")
 
window_right.mainloop()
window_left.mainloop()

Oh Doh me.... forgot add label name and pack()... now it's worked.


RE: DUAL WINDOWS... - DT2000 - Sep-27-2020

You did not define the Label nor pack or place the Label widget which needs to be done for it to be published to the screen.
Here it is with the font size changed to 12 so it is fits within the windows.

from tkinter import *

window_left = Tk()
window_left.geometry("300x300")
window_left.configure(bg='black')
window_left.title("LEFT")

window_right = Toplevel()
window_right.geometry("300x300")
window_right.configure(bg='black')
window_right.title("RIGHT")

win_left = Label(window_left, text="HELLO WORLD FROM LEFT", font=("Droid Sans Fallback", 12), fg="White", bg="black")
win_left.pack()

win_right = Label(window_right, text="HELLO WORLD FROM RIGHT", font=("Droid Sans Fallback", 12), fg="White", bg="black")
win_right.pack()

window_right.mainloop()
window_left.mainloop()



RE: DUAL WINDOWS... - ATARI_LIVE - Sep-27-2020

(Sep-27-2020, 04:21 AM)DT2000 Wrote: You did not define the Label nor pack or place the Label widget which needs to be done for it to be published to the screen.
Here it is with the font size changed to 12 so it is fits within the windows.

from tkinter import *

window_left = Tk()
window_left.geometry("300x300")
window_left.configure(bg='black')
window_left.title("LEFT")

window_right = Toplevel()
window_right.geometry("300x300")
window_right.configure(bg='black')
window_right.title("RIGHT")

win_left = Label(window_left, text="HELLO WORLD FROM LEFT", font=("Droid Sans Fallback", 12), fg="White", bg="black")
win_left.pack()

win_right = Label(window_right, text="HELLO WORLD FROM RIGHT", font=("Droid Sans Fallback", 12), fg="White", bg="black")
win_right.pack()

window_right.mainloop()
window_left.mainloop()

Yeah, I did it. I found out in before your replied. thanks anyway.