GUI in Python
GUI in Python
Although Tkinter is considered the de-facto Python GUI framework, it’s not
without criticism. One notable criticism is that GUIs built with Tkinter look
outdated. If you want a shiny, modern interface, then Tkinter may not be what
you’re looking for.
One of the nice things about filling the window with .pack() is that the fill is
responsive to window resizing. Try widening the window generated by the previous
script to see how this works. As you widen the window, the width of the
three Frame widgets grow to fill the window:
import tkinter as tk
window = tk.Tk()
frame1 = tk.Frame(master=window, width=200, height=100,
bg="red")
frame1.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
frame2 = tk.Frame(master=window, width=100, bg="yellow")
frame2.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
frame3 = tk.Frame(master=window, width=50, bg="blue")
frame3.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
window.mainloop()
The .place() Geometry Manager
• You can use .place() to control the precise
location that a widget should occupy in a window
or Frame. You must provide two keyword
arguments, x and y, which specify the x- and y-
coordinates for the top-left corner of the widget.
Both x and y are measured in pixels, not text units.
• The origin (where x and y are both 0) is the top-left
corner of the Frame or window. So, you can think of
the y argument of .place() as the number of pixels
from the top of the window, and the x argument as
the number of pixels from the left of the window.
Here’s an example of how the .place() geometry manager
works:
import tkinter as tk
window = tk.Tk()
frame = tk.Frame(master=window, width=150, height=150)
frame.pack()
label1 = tk.Label(master=frame, text="I'm at (0, 0)",
bg="red")
label1.place(x=0, y=0)
label2 = tk.Label(master=frame, text="I'm at (75, 75)",
bg="yellow")
label2.place(x=75, y=75)
window.mainloop()
.place() is not used often. It has two main
drawbacks:
• Layout can be difficult to manage
with .place(). This is especially true if your
application has lots of widgets.
• Layouts created with .place() are not
responsive. They don’t change as the window
is resized.
The .grid() Geometry Manager
import tkinter as tk
window = tk.Tk()
for i in range(3):
for j in range(3):
frame = tk.Frame(
master=window,
relief=tk.RAISED,
borderwidth=1 )
frame.grid(row=i, column=j)
label = tk.Label(
master=frame,
text=f"Row {i}\nColumn {j}")
label.pack()
window.mainloop()
Each label is attached to its master Frame with .pack():
import tkinter as tk
window = tk.Tk()
for i in range(3):
for j in range(3):
frame = tk.Frame( master=window, relief=tk.RAISED,
borderwidth=1 )
frame.grid(row=i, column=j)
label = tk.Label(master=frame, text=f"Row {i}\nColumn
{j}")
label.pack()
window.mainloop()
Using command
img1=ImageTk.PhotoImage(Image.open('i2.gif'))
l=Label(root,image=img1,width=600,height=400)
Open New Window
# Import required libraries
from tkinter import *
# Create an instance of tkinter window
win = Tk()
win.geometry("700x400")
win.title("Root Window")
# Function to create a toplevel window
def create_top():
top=Toplevel(win)
top.geometry("400x250")
top.title("Toplevel Window")
Label(top, text="Hello, Welcome to Tutorialspoint", font='Arial 15 bold').pack()
buttn=Button(top, text="Close", command=top.destroy)
buttn.pack()
# Create a button to open the toplevel window
b1=Button(text="new window",command=create_top)
b1.pack()
b2=Button(text="Close window",command=quit)
b2.pack()