Python tkinter label grid

Python Tkinter Grid (grid() method in Python Tkinter)

Want to know how to use the Python Tkinter grid? In this tutorial, we are covering everything on Grid in Python Tkinter. We will see how to use grid() method in Python Tkinter. Also, we will cover these topics:

  • Python Tkinter Grid
  • How to use Python Tkinter grid() method
  • Python Tkinter Grid Example
  • Python Tkinter Grid Align Left
  • Python Tkinter Grid Function
  • Python Tkinter Grid Padding
  • Python Tkinter Grid Columnspan
  • Python Tkinter Grid Spacing
  • Python Tkinter Grid Not Working
  • Python Tkinter Grid Expand
  • Python Tkinter Grid Fill

Python Tkinter Grid

  • Python Tkinter provides widgets that make the application appear good and easy to use for the users. Placement of widgets on the application is equally important as choosing the right widget.
  • Python Tkinter provides three types of Layout managers that help the developer to place the widget at the right spot on the application.
    • Pack: it packs all the widgets in the center and places widgets in a sequence.
    • Grid: Grid places widgets in a row and column-wise.
    • Place: Place positions widgets in X and Y coordinates.

    Python Tkinter Grid Example

    In this section, we will see an example for Grid Layout manager in Python Tkinter also we’ll see few features of grid layout manager in Python Tkinter.

    • Grid Layout manager requires rows and columns as an argument using this information it places the widget on the screen.
    • rows and columns start from the top left of the screen. You can visualize the spreadsheet to understand it better.
    • columnspan is used to occupy the other cells of columns like in the below example you can see that button is occupying two cells’ space.
    • Here is an example of implementing a grid on the widgets. In this example, we have used label, entry, and button widgets.

    grid() method in Python Tkinter

    Source code of the above example:

    In this example, we have placed the label widgets in the column 0, entry widgets at column 1. Whereas button is expanded to two columns.

    from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg='#9FD996') Label( ws, text='Enter Email', bg='#9FD996' ).grid(row=0, column=0) Label( ws, text='Enter Password', bg='#9FD996' ).grid(row=1, column=0) Entry(ws).grid(row=0, column=1) Entry(ws).grid(row=1, column=1) Button( ws, text='Login', command=None ).grid(row=2, columnspan=2) ws.mainloop()

    Python Tkinter Grid Align Left

    • The grid layout manager in Python Tkinter provides a sticky option that allows aligning the widgets to the LEFT, RIGHT, TOP, BOTTOM direction. In this section, we will see how to set alignment to Left using Grid in Python Tkinter.
    • Sticky requires the first letter of the universal direction in lower case to set the widget in the direction of choice. Here are the options
      • sticky = ‘e’ : Align to Right
      • sticky = ‘w’: Align to Left
      • sticky = ‘n’: Align to Top
      • sticky = ‘s’: Align to bottom

      Example of Python Tkinter Grid Align to Left:

      In this example, we have displayed two images. In the first image Label widget and Button widget are not aligned whereas in the second image we have aligned both the widget to left using sticky option in Grid layout manager of Python Tkinter.

      Python Tkinter Grid

      Python Tkinter Grid Function

      • Grid in Python Tkinter is a function that allows managing the layout of the widget. It takes rows and columns as an argument and places the widget in 2D format.
      • Due to its 2D property, Python Tkinter Grid is widely used for games and applications like Calculator, Tic-Tac-Toe, Mines, etc.
      • Apart from rows and columns, Grid offers a few more options:

      Python Tkinter Grid Padding

      • Grid in Python Tkinter provides a padding option using which space can be added inside as well as outside the widget’s border.
      • While organizing a widget at times the default size or position of the widget didn’t meet the developer’s expectation. At that time using padding, a developer adjusts the widget.
      • There are two types of padding:
        • Padding inside the widget’s border
        • Padding outside the widget’s border

        Example of Grid padding in Python Tkinter

        In this example, four pictures are displayed and each shows the change in button when padding is added. We have choose button 5 which is in the center of all the buttons.

        Image 1: image one don’t have any padding. The purpose of adding this image is to show you the original image so that you can compare it with the changed images.

        Image 2: We have added ipadx=10 and ipady=10 , with this 10 pixels are added inside the widget’s border and it has started looking bigger.

        Image 3: We have added padx=10 and pady=10 , with this 10 pixels are added outside the widget’s border and as a result of which other widgets have to move away.

        Image 4: Fourth image is the final image and the source code of same is provided below. In this we have applied both the padding, as a result of which button 5 is looking bigger and other widgets are shifted by 10 pixel.

        python tkinter grid padding

        Code Snippet for the above diagram

        Here is the code snippet for the above-mentioned program. In this code, we have provided both inside and outside widgets width. So this code will produce the output on the fourth picture. The exact code is available on line 15.

        from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg='#F2B33D') frame = Frame(ws, bg='#F2B33D') Button(frame, text="7").grid(row=0, column=0, sticky='ew') Button(frame, text="8").grid(row=0, column=1) Button(frame, text="9").grid(row=0, column=2) Button(frame, text="4 ").grid(row=1, column=0) Button(frame, text="5").grid(row=1, column=1, ipadx=10, ipady=10, padx=10, pady=10) Button(frame, text="6").grid(row=1, column=2) Button(frame, text="7 ").grid(row=2, column=0) Button(frame, text="8").grid(row=2, column=1) Button(frame, text="9").grid(row=2, column=2) frame.pack(expand=True) ws.mainloop()

        Python Tkinter Grid Columnspan

        Python Tkinter Grid Columnspan

        • Grid in Python Tkinter provides columnspan using which we can merge two or more cells into one.
        • columnspan is helpful in cases when you need extra space for a particular widget.
        • columnspan accepts an integer as an argument and merges the same number of cells together.

        Code Snippet:

        In this code, online 14 we have use columnspan=3, it has merged three cells into one and displayed button to it. The button is expanded to the available space because we have set sticky=’ew’.

        from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg='#F2B33D') frame = Frame(ws, bg='#F2B33D') Button(frame, text="These buttons").grid(row=0, column=0) Button(frame, text="are positioned").grid(row=0, column=1) Button(frame, text="using Grid").grid(row=0, column=2) Button(frame, text="another line").grid(row=1, columnspan=3, sticky='ew') frame.pack(expand=True) ws.mainloop()

        Python Tkinter Grid Spacing

Оцените статью