I\'m not very good with tkinter of python, but i would like to know if theres a way to make a window inside a window, where that window cannot get out of the main window\'s
There's nothing built-in to facilitate this, though there are enough building blocks to build your own. You can, for example, create a frame with some custom bindings that allow you to move it around its parent using the place
geometry manager.
There is no built in method of doing so. However I've made a work around to accommodate it. Keep in mind that when trying to move the sub window outside the main window it isn't a smooth lock so it does get jumpy. Another issue is that because of the configure events I can't get the sub window position relative to main window to maintain it while moving the main window. Still working around that. However the code below does work and should be of use to you.
import tkinter as tk
root = tk.Tk()
root.title("Main Window")
root.geometry("640x480")
sub = tk.Toplevel(root)
sub.transient(root) #Keeps sub window on top of root
sub.title('Sub Window')
sub.minsize(320, 240)
sub.maxsize(320, 240)
pos = []
def main_move(event):
#When the main window moves, adjust the sub window to move with it
if pos:
sub.geometry("+{0}+{1}".format(pos[0], pos[1]))
# Change pos[0] and pos[1] to defined values (eg 50) for fixed position from main
def sub_move(event):
# Set the min values
min_w = root.winfo_rootx()
min_h = root.winfo_rooty()
# Set the max values minus the buffer for window border
max_w = root.winfo_rootx() + root.winfo_width() - 15
max_h = root.winfo_rooty() + root.winfo_height() - 35
# Conditional statements to keep sub window inside main
if event.x < min_w:
sub.geometry("+{0}+{1}".format(min_w, event.y))
elif event.y < min_h:
sub.geometry("+{0}+{1}".format(event.x, min_h))
elif event.x + event.width > max_w:
sub.geometry("+{0}+{1}".format(max_w - event.width, event.y))
elif event.y + event.height > max_h:
sub.geometry("+{0}+{1}".format(event.x, max_h - event.height))
global pos
# Set the current sub window position
pos = [event.x, event.y]
root.bind('<Configure>', main_move)
sub.bind('<Configure>', sub_move)
root.mainloop()