Python Forum
[Tkinter] DUAL WINDOWS...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] DUAL WINDOWS...
#1
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.
Reply
#2
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.
Reply
#3
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()
"Often stumped... But never defeated."
Reply
#4
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.
Reply
#5
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()
"Often stumped... But never defeated."
Reply
#6
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dual Tkinter windows and shells Astrikor 6 3,907 Sep-03-2020, 10:09 PM
Last Post: Astrikor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020