[Tkinter] Forms' expansion - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Forms' expansion (/thread-18563.html) |
Forms' expansion - Gupi - May-22-2019 Hello everyone, I'm new in Python (especially tkinter) and I need an advice. I'd like to make 2 forms in a window, like it's shown on the image. 1. Form1 has relative width and height equal to height minus button's height. 2. Form2 has relative width and height equal to button's height. 3. Both forms are resizeable, since user has ability to resize the window. 4. When you resize the window, Form2's height stays constant (button's size = 26 px). RE: Forms' expansion - heiner55 - May-22-2019 Please post your code. RE: Forms' expansion - Yoriz - May-22-2019 Something like this ? import tkinter as tk class MainFrame(tk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.create_form1() self.create_form2() def create_form1(self): form1 = tk.Frame(self.master, background="pink") form1.pack(fill=tk.BOTH, expand=1) lbl = tk.Label(form1, text="Form1") lbl.pack(expand=1) def create_form2(self): form2 = tk.Frame(self.master, background="lightblue") form2.pack(fill=tk.BOTH) btn = tk.Button(form2, text="Form2") btn.pack() if __name__ == "__main__": tk_app = tk.Tk() main_frame = MainFrame() tk_app.mainloop() RE: Forms' expansion - Gupi - May-23-2019 (May-22-2019, 06:59 PM)Yoriz Wrote: Something like this ?Thank you! This is exactly what I was looking for. I've tried ".place" method for forms, but "relheight" is a constant value. Could you please explain the code, so I could understand what's what? The list of what I don't understand is: 1. Why did you give name for Tkinter in the beginning? In my code it worked better 2. What did you use class for? 3. What does "__init__" function do and why is it called so? 4. Purpose of the condition in the end of code and how does it work? My code is written below: from tkinter import * root = Tk() root.geometry("640x480") root.minsize(640, 480) frame_main = Frame(root, bg="blue", width=(root.winfo_width()), height=(root.winfo_height()*0.95)) frame_bottom = Frame(root, bg="yellow", width=(root.winfo_width()), height=(root.winfo_height() * 0.05)) frame_left = Frame(frame_main, bg="red", width=(frame_main.winfo_width()/2), height=(root.winfo_height())) frame_right = Frame(frame_main, bg="green", width=(frame_main.winfo_width()/2), height=(root.winfo_height())) def framemain(): frame_main.pack() frame_main.place(relwidth=1, relheight=0.95) def framebottom(): frame_bottom.pack() frame_bottom.place(relwidth=1, relheight=0.05, relx=0, rely=0.95) def frameleft(): frame_left.pack() frame_left.place(relwidth=0.5, relheight=1) def frameright(): frame_right.pack() frame_right.place(relwidth=0.5, relheight=1, relx=0.5) menu = Menu(root) root.config(menu=menu) menu.add_command(label="Check") def drawforms(): root.update_idletasks() framemain() framebottom() frameleft() frameright() drawforms() root.mainloop()Thanks in advance for reply! RE: Forms' expansion - Yoriz - May-23-2019
RE: Forms' expansion - Gupi - May-23-2019 Yoriz, thank you so much, you've been a BIG help! |