Python Tkinter widgets added to root window instead of Toplevel window [duplicate] - python

This question already has an answer here:
Python - Tkinter - Widgets created inside a class inherited from Toplevel() appear in a different frame OUTSIDE the class, Toplevel() class is empty
(1 answer)
Closed 5 years ago.
Using Python 2.7 here. I am trying to add a basic settings window, but when I open a Toplevel window and try to add widgets to it, the widgets get added to the main window instead. Here is an example:
import Tkinter as tk
class MainWindow (tk.Frame):
def __init__ (self, root):
tk.Frame.__init__(self, root)
self.root = root
self.root.geometry("300x200")
button = tk.Button(self, text="Settings", command=self.open_settings).pack()
def open_settings (self):
settings_win = tk.Toplevel(self.root, height=300, width=400)
settings_win.focus_set()
top_frame = tk.Frame(settings_win, bg="red").pack(side="top", fill="both", expand=True)
bottom_frame = tk.Frame(settings_win, bg="blue").pack(side="bottom", fill="both", expand=True)
top_label = tk.Label(top_frame, text="Top Label").pack()
bottom_label = tk.Label(bottom_frame, text="Bottom Label").pack()
if __name__ == '__main__':
root = tk.Tk()
MainWindow(root).pack(fill="both", expand=True)
root.mainloop()
Here is what I see when I click on the Settings button below. The second window opens but the labels show up on the main window.

its because your packing on the same line, check out the answer to this question, he explains it in detail: Python - Tkinter - Widgets created inside a class inherited from Toplevel() appear in a different frame OUTSIDE the class, Toplevel() class is empty
this should fix it:
top_frame = tk.Frame(settings_win, bg="red")
top_frame.pack(side="top", fill="both", expand=True)
bottom_frame = tk.Frame(settings_win, bg="blue")
bottom_frame.pack(side="bottom", fill="both", expand=True)
top_label = tk.Label(top_frame, text="Top Label")
top_label.pack()
bottom_label = tk.Label(bottom_frame, text="Bottom Label")
bottom_label.pack()
here is a screenshot:

Related

How to display Option menu inside a frame in Tkinter?

I am trying to display a bunch of OptionMenu in tkinter. The problem is that once there are too many OptionMenu they go out of screen and they cannot be accessed anymore.
So I thought of implementing a full-screen scrollbar to solve this.
I followed this tutorial - link, in this, the full-screen scrollbar is implemented by putting buttons inside a frame
The code from the tutorial - Working code with buttons
So I tried to use this code but instead of buttons, use OptionMenu.
This is my code
from tkinter import *
import tkinter as tk
from tkinter import ttk
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
master.title('ATOM')
master.geometry('650x650')
main_frame = Frame(root)
main_frame.pack(fill=BOTH, expand=1)
# Create A Canvas
my_canvas = Canvas(main_frame)
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
# Add A Scrollbar To The Canvas
my_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
# Configure The Canvas
my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))
# Create ANOTHER Frame INSIDE the Canvas
second_frame = Frame(my_canvas)
# Add that New frame To a Window In The Canvas
my_canvas.create_window((0,0), window=second_frame, anchor="nw")
length=[1,2,3,4,5,6,7,8,9,10]
variable_rsi_length = tk.StringVar(second_frame)
rsi_len = ttk.OptionMenu(second_frame, variable_rsi_length,*length )
variable_rsi_length.set('14')
for thing in range(100):
ttk.Button(second_frame, text=f'Button {thing} Yo!').grid(row=thing, column=0, pady=10, padx=10)
my_label = Label(second_frame, text="It's Friday Yo!").grid(row=3, column=2)
rsi_len.pack()
self.pack()
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
app.mainloop()
But this doesn't give any error on running in fact it does not even show the new window.
How can I implement this?
What's wrong is that you cannot use pack when its children are being managed by grid.
To be more specific, the error is: _tkinter.TclError: cannot use geometry manager pack inside .!frame.!canvas.!frame which already has slaves managed by grid
So, what you can easily do is just use one type of geometry manager.
Either use only "pack", or only "grid".
Here's a quick solution:
.
.
.
for thing in range(100):
ttk.Button(second_frame, text=f'Button {thing} Yo!').pack()
my_label = Label(second_frame, text="It's Friday Yo!").pack()
rsi_len.pack()
self.pack()
.
.
.

how could I make click events pass through a window using python?

I want to add overlays to my screen that displays some shapes using python and I am trying to achieve this by making a window transparent and make click events pass through the window. I am using python 3.6 (i can change the version if neccesary) and I am on windows 10.
Note: similar questions have been asked in the past here and here but neither answer my question.
Thanks in advance
Tkinter does not have passing events to parent widgets. But it is possible to simulate the effect through the use of bindtags
Making a binding to a widget is not equal to adding a binding to a widget. It is binding to a bindtag which has the same name as the widget, but it's not actually the widget.
Widgets have a list of bindtags, when an event happens on a widget, the bindings for each tag are processed.
import Tkinter as tk
class BuildCanvas(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.main = tk.Canvas(self, width=250, height=250,
borderwidth=0, highlightthickness=0,
background="linen")
self.main.pack(side="top", fill="both", expand=True)
self.main.bind("<1>", self.on_main_click)
for x in range(10):
for y in range(10):
canvas = tk.Canvas(self.main, width=35, height=35,
borderwidth=1, highlightthickness=0,
relief="groove")
if ((x+y)%2 == 0):
canvas.configure(bg="yellow")
self.main.create_window(x*45, y*45, anchor="nw", window=canvas)
bindtags = list(canvas.bindtags())
bindtags.insert(1, self.main)
canvas.bindtags(tuple(bindtags))
canvas.bind("<1>", self.on_sub_click)
def on_sub_click(self, event):
print("sub-canvas binding")
if event.widget.cget("background") == "yellow":
return "break"
def on_main_click(self, event):
print("main widget binding")
if __name__ == "__main__":
root = tk.Tk()
BuildCanvas(root).pack (fill="both", expand=True)
root.mainloop()

Are "self" and "root" different when creating buttons in a Python Class?

In the script below button_01 and button_02 are created in "self" and "root" respectively. Is there any functional difference where they are created? The GUI looks the same either way.
import Tkinter as tk
class App(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
frame1 = tk.Frame(root, padx=2, pady=2, borderwidth=2, relief="raised")
frame1.pack(side=tk.RIGHT)
button_01 = tk.Button(self, text ="tk Button 1") # self with tk.Button
button_01.config(width=15, fg="black", bg="lightskyblue")
button_01.pack(side=tk.BOTTOM)
button_02 = tk.Button(root, text ="tk Button 2") # root with tk.Button
button_02.config(width=15, fg="black", bg="lime")
button_02.pack(side=tk.BOTTOM)
button_03 = tk.Button(frame1, text ="tk Button 3") # frame1 with tk.Button
button_03.config(width=15, fg="black", bg="lightcoral")
button_03.pack(side=tk.TOP)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
app.pack(fill="both", expand=True)
#
root.mainloop()
No, self and root are not the same. Widgets live in a tree-like hierarchy, with a single root. When you call tk.Tk() you are creating this root window.
self represents the object to which the methods belong. In this case the widget is a subclass of tk.Frame which is a child of root.
Try giving the frame a background color (eg: self.configure(background="red") and you will see that the buttons have different parents. The gui looks the same in this specific example whether you use root or self only because it is an extremely simple gui with a very simple layout.

tkinter: scrollbar autohide without window resize

Using the folowing sample code I wrote I am having issues with some behavior.
I want to add/remove the scrollbar as needed. But when I do it shifts all other elements in the window as the window resizes. This is just a sample to demonstrate the issue, you will see the window resize when the scrollbar is added and removed. In the real application there are more widgets on the window.
Am I trying to do this the right way or if not how can I resolve the issue? I also plan to have a second widget with scrollbars as well in another separate frame.
from tkinter import *
from tkinter import ttk
class TopFrame(ttk.Frame):
def __init__(self, parent, col=0, row=0):
ttk.Frame.__init__(self, parent)
self.innerframe = ttk.Frame(parent)
self.list_scroll = ttk.Scrollbar(self.innerframe)
self.list_scroll.grid(column=1, row=0, sticky=NS)
self.list_scroll.grid_remove()
self.list = Listbox(self.innerframe, width=64, height=8,
yscrollcommand=self.list_scroll.set)
self.list_scroll.config(command=self.list.yview)
self.list.grid(column=0, row=0, sticky=NSEW)
self.innerframe.grid(column=col, row=row)
self.addbtn = ttk.Button(parent, text='add item',
command=self.additem)
self.addbtn.grid(column=col, row=row+1, padx=10, pady=2)
self.delbtn = ttk.Button(parent, text='del item',
command=self.delitem)
self.delbtn.grid(column=col, row=row+2, padx=10, pady=2)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
def additem(self):
count = str(len(self.list.get(0, END)))
self.list.insert(END, 'demo' + count)
if len(self.list.get(0, END)) > 8:
self.list_scroll.grid()
def delitem(self):
self.list.delete(END)
if len(self.list.get(0, END)) <= 8:
self.list_scroll.grid_remove()
class MasterFrame(Tk):
def __init__(self):
Tk.__init__(self)
topframe = TopFrame(self)
if __name__ == '__main__':
MasterFrame().mainloop()
Once the window has been displayed for the first time you can get the window size, and then use that to call the geometry method on the root window. When you set the size of the window with the geometry command it will stop resizing based on changes to its internal widgets.
The simplest thing is to write a function to do that, and schedule it to run with after_idle, which should fire after the window is first displayed.

Force set tkinter window to always have focus

Is there a way to tell Tkinter that I want some widget to always remain focused? I've created a minimal example that can be run to show my issue , here's an example window with small toplevel windows also overlayed:
Now if I click the upper title tk, the main window comes into focus and suddenly the small windows are behind the main window
I want to treat these smaller windows as if they are always in focus until the user specifically closes them. Of course this is a minimal example that is an idea behind a small subsection of my large application , is there any easy setting I can use for the toplevel that guarantees it will always remain in focus regardless of other windows? Here's the actual code that can be run to replicate this:
from Tkinter import *
class PropertyDialog(Toplevel):
def __init__(self, root, string):
Toplevel.__init__(self)
self.wm_overrideredirect(1)
self.root = root
self.\
geometry('+%d+%d' %
(root.winfo_pointerx(),
root.winfo_pointery()))
try:
self.tk.call('::Tk::unsupported::MacWindowStyle',
'style', self._w,
'help', 'noActivates')
except TclError:
pass
window_frame = Frame(self)
window_frame.pack(side=TOP, fill=BOTH, expand=True)
exit_frame = Frame(window_frame, background='#ffffe0')
exit_frame.pack(side=TOP, fill=X, expand=True)
button = Button(exit_frame, text='x', width=3, command=self.free,
background='#ffffe0', highlightthickness=0, relief=FLAT)
button.pack(side=RIGHT)
text_frame = Frame(window_frame)
text_frame.pack(side=TOP, fill=BOTH, expand=True)
label = Label(text_frame, text=string, justify=LEFT,
background='#ffffe0',
font=('tahoma', '8', 'normal'))
label.pack(ipadx=1)
def free(self):
self.destroy() # first we destroy this one
for val,widget in enumerate(dialogs): # go through the dialogs list
if widget is self: # when we find this widget
dialogs.pop(val) # pop it out
break # and stop searching
if dialogs: # if there are any dialogs left:
for widget in dialogs: # go through each widget
widget.lift(aboveThis=self.root) # and lift it above the root
def bind():
"""
toggle property window creation mode
"""
root.bind('<ButtonPress-1>', create)
def create(event):
"""
Create actual window upon mouse click
"""
dialogs.append(PropertyDialog(root, 'help me'))
root = Tk()
dialogs = []
root.geometry('%dx%d' % (300,400))
Button(root, text='create', command=bind).pack()
root.mainloop()
change this:
if dialogs: # if there are any dialogs left:
for widget in dialogs: # go through each widget
widget.lift(aboveThis=self.root) # and lift it above the root
to this:
if dialogs: # if there are any dialogs left:
for widget in dialogs: # go through each widget
widget.lift() # and lift it above the root
the widgets will stay above the main window.
EDIT:
Sorry that only half worked... the widows will stay above sometimes with that code
:-X
It was keeping the widgets on top until you closed one of them.... this code does keep the widgets on top
it uses the self.attributes("-topmost", True) when you spawn the windows.
Sorry again.
from Tkinter import *
class PropertyDialog(Toplevel):
def __init__(self, root, string):
Toplevel.__init__(self)
self.wm_overrideredirect(1)
self.root = root
self.\
geometry('+%d+%d' %
(root.winfo_pointerx(),
root.winfo_pointery()))
try:
self.tk.call('::Tk::unsupported::MacWindowStyle',
'style', self._w,
'help', 'noActivates')
except TclError:
pass
window_frame = Frame(self)
window_frame.pack(side=TOP, fill=BOTH, expand=True)
exit_frame = Frame(window_frame, background='#ffffe0')
exit_frame.pack(side=TOP, fill=X, expand=True)
button = Button(exit_frame, text='x', width=3, command=self.free,
background='#ffffe0', highlightthickness=0, relief=FLAT)
button.pack(side=RIGHT)
text_frame = Frame(window_frame)
text_frame.pack(side=TOP, fill=BOTH, expand=True)
label = Label(text_frame, text=string, justify=LEFT,
background='#ffffe0',
font=('tahoma', '8', 'normal'))
label.pack(ipadx=1)
self.attributes("-topmost", True)
def free(self):
self.destroy() # first we destroy this one
def bind():
"""
toggle property window creation mode
"""
root.bind('<ButtonPress-1>', create)
def create(event):
"""
Create actual window upon mouse click
"""
dialogs.append(PropertyDialog(root, 'help me'))
root = Tk()
dialogs = []
root.geometry('%dx%d' % (300,400))
Button(root, text='create', command=bind).pack()
root.mainloop()
I recommend moving away from Toplevel widgets, since those are separate windows and you're suppressing their window-like behavior. This version makes PropertyDialog inherit from Frame instead of Toplevel, using the place() geometry manager. When you click the main window, it first checks whether the widget clicked was the main window or a popup window to prevent a new popup from appearing when you close an existing one. Changed areas are marked with #CHANGED#.
from Tkinter import *
class PropertyDialog(Frame): #CHANGED#
def __init__(self, root, string, event): #CHANGED#
Frame.__init__(self) #CHANGED#
self.root = root
try:
self.tk.call('::Tk::unsupported::MacWindowStyle',
'style', self._w,
'help', 'noActivates')
except TclError:
pass
exit_frame = Frame(self, background='#ffffe0') #CHANGED#
exit_frame.pack(side=TOP, fill=X, expand=True)
button = Button(exit_frame, text='x', width=3, command=self.free,
background='#ffffe0', highlightthickness=0, relief=FLAT)
button.pack(side=RIGHT)
text_frame = Frame(self) #CHANGED#
text_frame.pack(side=TOP, fill=BOTH, expand=True)
label = Label(text_frame, text=string, justify=LEFT,
background='#ffffe0',
font=('tahoma', '8', 'normal'))
label.pack(ipadx=1)
self.place(x=event.x, y=event.y, anchor=NW) #CHANGED#
def free(self):
self.destroy()
# other things you want to do - if there's nothing else,
# just bind the close button to self.destroy
def bind():
"""
toggle property window creation mode
"""
root.bind('<ButtonPress-1>', create)
def create(event):
"""
Create actual window upon mouse click
"""
if event.widget is root: #CHANGED#
dialogs.append(PropertyDialog(root, 'help me', event))
root = Tk()
dialogs = []
root.geometry('%dx%d' % (300,400))
Button(root, text='create', command=bind).pack()
root.mainloop()

Categories