Pin-Jiun / Python

Python Document
0 stars 0 forks source link

14-GUI layout #14

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago
S No. | Widget | Description -- | -- | -- 1 | pack() | This geometry manager organizes widgets in blocks before placing them in the parent widget. 2 | grid() | This geometry manager organizes widgets in a table-like structure in the parent widget. 3 | place() | This geometry manager organizes widgets by placing them in a specific position in the parent widget.

place()

The Place geometry manager is the simplest of the three general geometry managers provided in Tkinter. It allows you explicitly set the position and size of a window, either in absolute terms, or relative to another window. https://www.geeksforgeeks.org/python-place-method-in-tkinter/

# Importing tkinter module
from tkinter import *
from tkinter.ttk import *

# creating Tk window
master = Tk()

# setting geometry of tk window
master.geometry("200x200")

# button widget
b2 = Button(master, text = "GFG")
b2.pack(fill = X, expand = True, ipady = 10)

# button widget
b1 = Button(master, text = "Click me !")

# This is where b1 is placed inside b2 with in_ option
b1.place(in_= b2, relx = 0.5, rely = 0.5, anchor = CENTER)

# label widget
l = Label(master, text = "I'm a Label")
l.place(anchor = NW)

# infinite loop which is required to
# run tkinter program infinitely
# until an interrupt occurs
mainloop()

grid()

The Grid geometry manager puts the widgets in a 2-dimensional table.

https://www.geeksforgeeks.org/python-grid-method-in-tkinter/ image

import tkinter as tk

win = tk.Tk()
win.title("test")
win.geometry("+800+400")
win.config(bg="#323232")

user = tk.Label(text="User")
user.grid(row=0, column=0)

password = tk.Label(text="Password ")
password.grid(row=1,column=0)

uesr_entry = tk.Entry(bg="#323232")
uesr_entry.grid(row=0,column=1)

password_entry = tk.Entry(bg="#323232")
password_entry.grid(row=1,column=1)

#將主視窗win常駐在畫面上
win.mainloop()
# import tkinter module
from tkinter import * from tkinter.ttk import *

# creating main tkinter window/toplevel
master = Tk()

# this will create a label widget
l1 = Label(master, text = "Height")
l2 = Label(master, text = "Width")

# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row = 0, column = 0, sticky = W, pady = 2)
l2.grid(row = 1, column = 0, sticky = W, pady = 2)

# entry widgets, used to take entry from user
e1 = Entry(master)
e2 = Entry(master)

# this will arrange entry widgets
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)

# checkbutton widget
c1 = Checkbutton(master, text = "Preserve")
c1.grid(row = 2, column = 0, sticky = W, columnspan = 2)

# adding image (remember image should be PNG and not JPG)
img = PhotoImage(file = r"C:\Users\Admin\Pictures\capture1.png")
img1 = img.subsample(2, 2)

# setting image with the help of label
Label(master, image = img1).grid(row = 0, column = 2,
    columnspan = 2, rowspan = 2, padx = 5, pady = 5)

# button widget
b1 = Button(master, text = "Zoom in")
b2 = Button(master, text = "Zoom out")

# arranging button widgets
b1.grid(row = 2, column = 2, sticky = E)
b2.grid(row = 2, column = 3, sticky = E)

# infinite loop which can be terminated
# by keyboard or mouse interrupt
mainloop()

Pack()

The Pack geometry manager packs widgets relative to the earlier widget. https://www.geeksforgeeks.org/python-pack-method-in-tkinter/

# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *

# creating Tk window
master = Tk()

# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)

# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
            background = "red", fg = "white")
b1.pack(side = TOP, expand = True, fill = BOTH)

# Button 2
b2 = Button(pane, text = "Click me too",
            background = "blue", fg = "white")
b2.pack(side = TOP, expand = True, fill = BOTH)

# Button 3
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = TOP, expand = True, fill = BOTH)

# Execute Tkinter
master.mainloop()

image

# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *

# creating Tk window
master = Tk()

# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)

# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
            background = "red", fg = "white")
b1.pack(side = LEFT, expand = True, fill = BOTH)

# Button 2
b2 = Button(pane, text = "Click me too",
            background = "blue", fg = "white")
b2.pack(side = LEFT, expand = True, fill = BOTH)

# Button 3
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = LEFT, expand = True, fill = BOTH)

# Execute Tkinter
master.mainloop()

image