'Resizing a toplevel window in a class as the parent window resizes
I have a class that creates new windows and sticks those windows to the main window, but now I need to figure out how to resize the new windows with the main window.
My issue is that the class I have creates new windows in a way I am not familiar with, so I have no idea how to call on the different Toplevel windows that get created in order to change their window size.
import tkinter as tk
from tkinter import ttk
class Main(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('500x500')
self.drag_id = ''
self.button_notes = ['tree view 1', 'tree view 2', 'tree view 3', 'tree view 4']
self.bind('<Configure>', self.dragging)
# self.bind("<Configure>", self.resize) # this event that should cause the new
# window to resize
self.pop_up_list = []
for ndex, value in enumerate(self.button_notes):
print(ndex)
btn = tk.Button(self, text=f'Button {ndex+1}')
btn.config(command=lambda b=btn, i=ndex: self.toggle_button_pop_ups(i, b))
btn.grid(row=ndex, column=0, padx=5, pady=5)
self.pop_up_list.append([value, 0, None, btn])
# def resize(self, e):
# p = self.pop_up_list
# print("height", e.height, "width", e.width)
# p[ndex][2].geometry("100x100") this is the part I need help with
def dragging(self, event):
if event.widget is self:
if self.drag_id == '':
pass
else:
self.after_cancel(self.drag_id)
self.drag_id = self.after(100, self.stop_drag)
for p in self.pop_up_list:
if p[1] == 1:
p[2].lift()
print(p[2])
p[2].geometry(f"+{p[3].winfo_rootx() + 65}+{p[3].winfo_rooty()}")
def stop_drag(self):
self.drag_id = ''
for p in self.pop_up_list:
if p[1] == 1:
p[2].lift()
p[2].geometry(f"+{p[3].winfo_rootx() + 65}+{p[3].winfo_rooty()}")
def toggle_button_pop_ups(self, ndex, btn):
p = self.pop_up_list
if p[ndex][1] == 0:
p[ndex][1] = 1
p[ndex][2] = tk.Toplevel(self)
n = id(p[ndex][2])
tree_view = ttk.Treeview(p[ndex][2], columns=("c1", "c2", "c3", "c4"),
show="headings", height=14)
tree_view.column("# 1", width=90, anchor=tk.CENTER)
tree_view.heading("# 1", text="something")
tree_view.column("# 2", width=90, anchor=tk.CENTER)
tree_view.heading("# 2", text="something 2")
tree_view.column("# 3", width=90, anchor=tk.CENTER)
tree_view.heading("# 3", text="something 3")
tree_view.column("# 4", width=90, anchor=tk.CENTER)
tree_view.heading("# 4", text="something 4")
tree_view.grid(column=2, row=2)
p[ndex][2].overrideredirect(1)
p[ndex][2].geometry(f"+{btn.winfo_rootx() + 65}+{btn.winfo_rooty()}")
else:
p[ndex][1] = 0
p[ndex][2].destroy()
p[ndex][2] = None
if __name__ == '__main__':
Main().mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
