from tkinter import * from tkinter.ttk import * root = Tk() # Style() padding adds pixels inside the Button. In this example, a ttk.button is positioned with pack() and is padded. Keep in mind that while Ttk widgets have a Style()padding option, it does not affect widget positioning. Important: The basic idea is to separate as much as possible the code implementing a widget’s behavior from the code implementing its appearance ( Style() ). Importing tkinter.ttk after importing tkinter causes Ttk widgets to automatically replace the standard Tk widgets of Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton, PanedWindow, Radiobutton, Scale and Scrollbar. To override Tk widgets, the ttk import must follow the tk import: from tkinter import * from tkinter.ttk import * To start using Ttk, enter the following import statement in a Python console: from tkinter import ttk However, positioning with the pack, place and grid layout managers remains the same as Tk. There are differences in how the Ttk theme is coded, including alignment and padding options for the six new widgets. The tkinter.ttk (Tile extension integrated into Tk) module provides access to the Tk themed widget set included in Tk 8.5 and greater. In this example, four labels are positioned in a grid of rows and columns: from tkinter import * root = Tk() Label(text="Position 1", width=10).grid(row=0, column=0) Label(text="Position 2", width=10).grid(row=0, column=1) Label(text="Position 3", width=10).grid(row=1, column=0) Label(text="Position 4", width=10).grid(row=1, column=1) root.mainloop() Grid() positions widgets in a two dimensional grid of rows and columns similar to a spreadsheet. Positioning Widgets With Grid Layout Manager In this example, three labels are positioned diagonally using x,y coordinates. Place() lets you position a widget either with absolute x,y coordinates, or relative to another widget. Positioning Widgets With Place Layout Manager In this example, three labels are positioned side by side in relation to each other, and are padded to separate them: import tkinter as tk root = tk.Tk() test = tk.Label(root, text="red", bg="red", fg="white") test.pack(padx=5, pady=15, side=tk.LEFT) test = tk.Label(root, text="green", bg="green", fg="white") test.pack(padx=5, pady=20, side=tk.LEFT) test = tk.Label(root, text="purple", bg="purple", fg="white") test.pack(padx=5, pady=20, side=tk.LEFT) root.mainloop() import tkinter as tk root = tk.Tk() test = tk.Label(root, text="Red", bg="red", fg="white") test.pack(side=tk.BOTTOM) test = tk.Label(root, text="Green", bg="green", fg="white") test.pack(side=tk.BOTTOM) test = tk.Label(root, text="Purple", bg="purple", fg="white") test.pack(side=tk.BOTTOM) tk.mainloop() In this example, three labels (widgets that contain text or images) are positioned vertically in relation to each other, and are not padded. For simple positioning of widgets vertically or horizontally in relation to each other, pack() is the layout manager of choice.įor more information about pack() and its options, refer to: How To Position Buttons in Tkinter With Pack Vertical Positioning with Pack However, pack() is limited in precision compared to place() and grid() which feature absolute positioning. Instead of declaring the precise location of a widget, pack() declares the positioning of widgets in relation to each other. Pack is the easiest layout manager to use with Tkinter. Positioning Widgets With the Pack Layout Manager Watch the video, which introduces the pack, place and grid code snippets below Important : pack(), place(), and grid() should not be combined in the same master window. grid() locates widgets in a two dimensional grid using row and column absolute coordinates.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |