tkinter.Button.grid


Definition
Button is positioned in the grid in the widget.

Syntax
tkinter.Button.grid
Button.grid

Options
  • Button.grid(
  • column=number - use cell identified with given column (starting with 0)
  • columnspan=number - this widget will span several columns
  • in=master - use master to contain this widget
  • in_=master - see 'in' option description
  • ipadx=amount - add internal padding in x direction
  • ipady=amount - add internal padding in y direction
  • padx=amount - add padding in x direction
  • pady=amount - add padding in y direction
  • row=number - use cell identified with given row (starting with 0)
  • rowspan=number - this widget will span several rows
  • sticky=NSEW - if cell is larger on which sides will this widget stick to the cell boundary
  • )

Examples
The code created Label, Entry, and button with defined parameters of row and column in grid.
 from tkinter import*

master=Tk()
master.title("Grid Widget")

label=Label(master, text="First Name",font=10)
label.grid(row=0)
entry = Entry(master)
entry.grid(row=0, column=1)


label=Label(master, text="Last Name", font=10)
label.grid(row=1)
entry = Entry(master)
entry.grid(row=1, column=1)

button1=Button(master, text="Submit")
button1.grid(row=2, column=1)

master.mainloop()





The method return with the Buttons positioned in the grid.
from tkinter import*
master=Tk()
master.grid()

button1=Button(master,text="B1")
button1.grid(row=1,column=0)

button2=Button(master, text="B2")
button2.grid(row=1,column=2)

button3=Button(master,text="B3")
button3.grid(row=0,column=1)

button4=Button(master,text="B4")
button4.grid(row=2,column=1)

master.mainloop()





The similarly the returns are:
 
from tkinter import*
master=Tk()

button1=Button(master,text="B1")
button1.grid(row=1,column=1)

button2=Button(master, text="B2")
button2.grid(row=1,column=2)

button3=Button(master,text="B3")
button3.grid(row=1,column=3)

button4=Button(master,text="B4")
button4.grid(row=1,column=4)

button5=Button(master,text="B5")
button5.grid(row=1,column=5)

master.mainloop()





Button.grid
The Entry.get() input text verified through Button(command).
from tkinter import*

master=Tk()
master.title("Login")

def validate1():
    if  entry.get()=="pythonlake":
        label1=Label(master, text="Welcome")
        label1.grid(row=0,column=3)
        entry.destroy()
        button.destroy()
        label.destroy()
    else:
        label2=Label(master, text="You are not authorized", background="red", fg="white")
        label2.grid(row=0,column=3)

label=Label(master, text="Enter your username: ")
label.grid(row=0,column=0)
           
entry=Entry(master)
entry.grid(row=0,column=1)

button=Button(master, text="Submit",command=validate1)
button.grid(row=0,column=2)

master.mainloop()









Comments