Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

python - Window inside window

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 bounds.

Heres my current code:

from tkinter import *

root = Tk()
root.title("Main Window")
root.geometry("640x480+100+100")

sub = Toplevel(root)
sub.title("Sub Window")
sub.geometry("320x240+125+125")

mainloop()

it would look like this: enter image description here

I would like to know how I can isolate the Sub Window to keep it inside the main window even if i drag it out.

Thank you very much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...