# Create Buttons, one to check which colors are checked,īutton(root, text="Tally", command=display_checked).grid(row=5)īutton(root, text="End", command=root.quit).grid(row=6)įirst we start off by importing Tkinter and setting up the root window in lines 1-5. Label = Label(root, text="Which colors do you like below?")Ĭheckbutton(root, width=10, text="red", variable=red_var, bg="red").grid(row=1)Ĭheckbutton(root, width=10, text="yellow", variable=yellow_var, bg="yellow").grid(row=2)Ĭheckbutton(root, width=10, text="green", variable=green_var, bg="green").grid(row=3)Ĭheckbutton(root, width=10, text="blue", variable=blue_var, bg="blue").grid(row=4) '''check if the checkbuttons have been toggled, and displayĪ value of '1' if they are checked, '0' if not checked''' Root.geometry("210x180") # set starting size of window In the following GUI, we combine Label, Checkbutton, and Button widgets to survey which of the colors people like.Įxample Tkinter UI using grid with Survey that asks which colors you like. The following is just a simple example to see how to set up our window and use grid(). Default is to center the widget within the "cell". If you use W+E+N+S, then the widget will fill up the "cell". The parameter tells which side of the "cell" the widget will "stick" to. sticky - specifies a value of S, N, E, W, or a combination of them, e.g.ipadx, ipady - how many pixels to use for padding inside the widget, also for horizontal or vertical padding.padx, pady - the number of pixels surrounding the widget to create padding between other widgets, for horizontal or vertical padding.This is very useful to help get the spacing right for your widgets. columnspan, rowspan - specifies how many columns or rows a widget will be in.row, column - the row and column values for the location of the widget.Now let's look at some of the main parameters that can help you arrange widgets using the grid layout manager. Widgets can also span across multiple rows or columns. Notice above how each widget can be positioned within "cells" by specifying their row and column values. How the grid layout manager works in Tkinter. Check out the image below for a visual example. It is similar if you want to move down a column, with the column value staying the same and the row value increasing. ![]() If you want to place widgets in a horizontal row, then the row value stays the same and the column value increases by 1 for each widget. So moving around the grid and arranging widgets is quite simple. The upper left-most corner has row value 0 and column value 0. The reason is because it works similar to a matrix, with rows and columns. I personally think that using the grid layout manager is the easiest manager out of the three managers that Tkinter offers. So for this post, I really want to just jump right in and help you to see more examples of designing UIs with Tkinter. In previous tutorials we talked about designing your UI layout. In this tutorial you are going to take a more detailed look at understanding how to arrange widgets with Tkinter's grid layout manager.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |