Python Forum
[Tkinter] Forms' expansion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Forms' expansion
#1
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).

[Image: canvas.jpg]
Reply
#2
Please post your code.
Reply
#3
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()
Reply
#4
(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!
Reply
#5

  1. Q: Why did you give name for Tkinter in the beginning? In my code it worked better
    A: see the following thread https://python-forum.io/Thread-Namespace...th-imports

  2. Q: What did you use class for?
    A: Classes are a much better way of organising GUI code and easier to access the instance attributes.

  3. Q: What does "__init__" function do and why is it called so?
    A: When an instance of a class is created the __init__ method is automatically called to initialise attributes, see forum tutorial https://python-forum.io/Thread-Classes-Class-Basics

  4. Q: Purpose of the condition in the end of code and how does it work?
    A: if __name__ == "__main__" is true if you are executing the module directly. This is used to put code that you want to run only when executing this module directly but not to execute if you are importing this module into another.
Reply
#6
Yoriz, thank you so much, you've been a BIG help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about data grid for forms - desktop program Everest 5 3,462 Apr-18-2019, 11:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020