Sunday, June 14, 2015

tk1. Frames in Tkinter

Tkinter is one of the GUI systems that we can use with Python, which is included in the default Python. In these examples, Python 3+ is used. For Python 2.7, you have to change the import names.


Almost any GUI application will use Frames, to embed other widgets such as Labels and Buttons. The only function of this program is to create 4 Frames in a grid arrangement.


After the import(s), we have to define an instance or the Tk class, as the root Widget. Two of its properties are then set.


For frame1 to frame4, we set three properties, width, height, and background color. We can use config function to set many properties at once.


Next, we layout the grid and start our loop.

# tk1.py
from tkinter import *
root = Tk()
root.title('The Frame')
root.geometry('500x500')
frame1 = Frame(root)
frame1['bg'] = 'red'
frame1['width'] = '240'
frame1['height'] = '240'
frame2 = Frame(root)
frame2.config(bg = 'blue', width = '240', height = '240')
frame3 = Frame(root)
frame3.config(bg = 'green', width = '240', height = '240')
frame4 = Frame(root)
frame4.config(bg = 'purple', width = '240', height = '240')
frame1.grid(row = 0, column = 0)
frame2.grid(row = 1, column = 0)
frame3.grid(row = 0, column = 1)
frame4.grid(row = 1, column = 1)
root.mainloop()

Output:

No comments:

Post a Comment