Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you combine two tkinter tk widgets?

I am creating an app which will have two Tk() widgets. Is it possible to combine them side into one larger widget side by side to make the app easier to use?

from tkinter import *

tk = Tk()
canvas = Canvas(tk,width=400, height=150)
canvas.pack()

tk2 = Tk()
canvas2 = Canvas(tk2,width=400, height=150)
canvas2.pack()

tk.mainloop(), tk2.mainloop()

When I do this to make the basic windows, I obviously get two seperate windows. Can that be combined into one?

I am a beginner and am using python 3.3

like image 349
user2155059 Avatar asked Dec 06 '25 00:12

user2155059


1 Answers

Not sure if it is what you are looking for, but you can create two Frames inside your main Tk.

import tkinter as tk

class SubWindow(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        x = tk.Text(self)
        x.pack()

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self)
        self.win1 = SubWindow(self)
        self.win1.pack(side="left")
        self.win2 = SubWindow(self)
        self.win2.pack(side="right")

if __name__ == "__main__":
    main = MainWindow()
    main.mainloop()

EDIT: Here is the code to make Frames resize when window does:

import tkinter as tk

class SubWindow(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        x = tk.Text(self)
        x.pack(expand=1, fill='both')

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self)
        self.win1 = SubWindow(self)
        self.win1.pack(side="left", expand=1, fill=tk.BOTH)
        self.win2 = SubWindow(self)
    self.win2.pack(side="right", expand=1, fill=tk.BOTH)

if __name__ == "__main__":
    main = MainWindow()
    main.mainloop()
like image 140
JadedTuna Avatar answered Dec 08 '25 14:12

JadedTuna