I wanted to ask if it is possible to draw a child window inside a parent window so that it will only be able to move inside the parent window and won't be able to move out of the parent window bounds.
If you mean having actual windows with title bar, menu, status bar etc. inside the parent window then the answer is:
No, Tcl/Tk and by extension Tkinter does not support this with its standard widgets.
There have been efforts in the past to implement widgets which emulate MDI as you can see on the TCL wiki, but most of them are over a decade old. You will probably have to implement it yourself or choose a different GUI toolkit if you really need to implement this kind of UI design.
If you do it yourself, you can use the Frame widget as the subwindow, and use place to put it in the containing window. Or, you can create it as an object on a canvas. You'll have to write all of the code to give the inner window borders and a title bar, and to manage moving it around, iconifying it, etc.
Related
I am working on a Gtk+ 3 application that is likely to be used fullscreen most of the time, but needs to be switched between fullscreen and non-fullscreen, while maintaining access to the controls located in the header bar. The problem is, since the headerbar is part of the window decorations, it gets hidden when the window goes fullscreen.
My current kludge so ensure the controls are always available works like this:
Setup
create a Gtk.Window with vertical Gtk.Box as first child
create a custom Gtk.HeaderBar (w/ added full screen togglebutton)
set window's titlebar as my custom Gtk.HeaderBar
add all the window's content to the Gtk.Box
When window goes fullscreen
remove the Gtk.HeaderBar from the Gtk.Window titlebar
pack the Gtk.HeaderBar into the Gtk.Box (window's first child).
This results in the Gtk.HeaderBar being at the bottom of the window, so
re-position the Gtk.HeaderBar to the top of the Gtk.Box
When the window goes un-fullscreen
remove the Gtk.HeaderBar from the gtk.Box
set it as the Gtk.Window's titlebar
This results in the following Gtk-warning: gtk_window_set_titlebar() called on a realized window (who cares, just a warning)
This works, but it seems like very much of a hack, and more complicated than it should have to be. Am I missing something and there is a more straightforward approach?
I know several Gtk+ 3 based apps have the header bar behavior I am after (gedit for example), but I have not been able to determine how that is implemented. Any insight would be greatly appreciated.
Also, here is a GitHub gist with a full working example of my current hacky solution: https://gist.github.com/KurtJacobson/6b045b6fc38907a2f18c38f6de2929e3
I will accept answers in any (programming) language.
I know that I can subclass a tk.Frame (or ttk.Frame) and add that to a TopLevel to make secondary windows, but I'm not sure how I should use that as the main window. I know that creating an instance of a Frame class and calling .mainloop() on it seems to work for using it as the main window, but I feel like that's bad practice...
What do other people do when they are making GUI layouts that they want to have available to main windows and secondary windows?
Create a subclass of a Frame, and then put it either in the root window or a toplevel. In either case, you still call mainloop only once on the root window.
The only care you have to take is that you have to be careful about letting the user close the root window, because it will cause all of the other windows to be destroyed.
If you're creating a program that can have multiple windows, you might want to consider hiding the root window and always putting your window in a Toplevel. Of course, when you do that you need to make sure you destroy the root window whenever the last toplevel window is destroyed, or your program will continue to run but the user will have no way to access it.
Do you mean having a home screen that you can flip back to? If so, you can try looking here: Using buttons in Tkinter to navigate to different pages of the application?
Is there a python library that allows you to create a GUI without the window manager (and equivalent under MS Windows) frame thing, and allow to set the window top-most (like over all other windows) ?
I've been looking on Internet but I'm obviously missing the right keywords.
Yup. Tkinter, or "tkinter" in python3. It's included in standard python distributions
Removing the Window Manager
The method
root.overrideredirect(True)
Removes the borders and manager. Setting the flag indicates to the manager that you don't want the widget to be managed. You can look at ttk, an extension to tkinter commonly included (in Python3 it's tkinter.ttk) if you don't want to completely remove the border, but just pick a different style.
Setting the TopMost Attribute
The method
root.wm_attributes("-topmost", 1)
Will put it at the top of the screen Combined, these will give you the desired behavior. You can then use other tkinter frames to decorate the widget as you please, i.e., add custom borders or close/minus buttons that would normally be included in the manager.
Resources
See this, e.g. for applying the topmost attribute
Make a tkinter window appear over all other windows
From Effbot about "overridedirect" (http://effbot.org/tkinterbook/wm.htm):
Sets or gets the override redirect flag. If non-zero, this prevents
the window manager from decorating the window. In other words, the
window will not have a title or a border, and it cannot be moved or
closed via ordinary means.
Note:
These functions can only be applied to the root window (Tk instance) or another Toplevel instance.
I wanted to ask if it is possible to draw a child window inside a parent window so that it will only be able to move inside the parent window and won't be able to move out of the parent window bounds.
If you mean having actual windows with title bar, menu, status bar etc. inside the parent window then the answer is:
No, Tcl/Tk and by extension Tkinter does not support this with its standard widgets.
There have been efforts in the past to implement widgets which emulate MDI as you can see on the TCL wiki, but most of them are over a decade old. You will probably have to implement it yourself or choose a different GUI toolkit if you really need to implement this kind of UI design.
If you do it yourself, you can use the Frame widget as the subwindow, and use place to put it in the containing window. Or, you can create it as an object on a canvas. You'll have to write all of the code to give the inner window borders and a title bar, and to manage moving it around, iconifying it, etc.
I need to make a desktop container kind of frame in Python so that from the menu bar I choose the option the window would open in that desktop pane, Like we use to deal with the Professional Applications.
Here is the Snapshot of the GUI. In the Snapshot the above Menu bar has lots of cascaded options as well. As the User choose the cascaded option, a new window should open below the Menu bar (Empty space in snap), and when the other option is chosen the previous frame should also be there and these sub frames should also have close,minimize and maximize button (e.g. Minimize Minimizes to the bottom of the frame space above the status bar
You are trying to create what is commonly referred to as a "Multiple Document Interface", or MDI. This was popular back in the 80's and early 90's, but is now generally considered to provide very poor usability.
Tkinter does not natively support MDI applications. You will have to do all the window management yourself. Generally this means that you'll use a canvas for the containing window, and then embed frames within the canvas to represent your windows. You'll have to draw your own window borders and handle resizing. I've seen it done, but it's usually not worth the effort to implement.