hossein
hossein

Reputation: 9

place tkinter buttons in a row

i want to place my buttons in a raw beside each other but when i set row same and change columns they place too far of each other.i tried to remove grid and add pack() but again i failed.changing pady didn't help me too.what should i do

import tkinter as tk
from tkinter import filedialog


root = tk.Tk()
root.geometry("810x490")
root.configure(background='#DCDCDC')
root.title('Hossein Fallah')


def getText():
    output_text.delete("1.0","end")
    result=input_text.get("1.0","end")
    output_text.insert(tk.END,result)
    return result
    
def textClear():
    output_text.delete("1.0","end")
    input_text.delete("1.0","end")

def fileDialog():
 
    filename = filedialog.askopenfilename(initialdir =  "/", title = "Select A File", filetype =
    (("text files","*.txt"),("all files","*.*")) )

# first text wiget
input_text=tk.Text(root, height=10,width=100,bd=5)
input_text.grid(row = 1, column = 0, pady = 2)

#BTNs
btnRead=tk.Button(root, height=1, width=20, text="ثبت",relief='flat',overrelief='groove',
                    command=getText)
btnRead.grid(row = 2, column = 0,  pady = 2)

btnClear=tk.Button(root, height=1, width=20, text="پاک کردن",relief='flat',overrelief='groove',
                    command=textClear)
btnClear.grid(row = 3, column = 0,  pady = 2)

btnFind =tk.Button(root, height=1, width=20,text = "پیدا کردن فایل",relief='flat',overrelief='groove',
                    command =fileDialog)
btnFind.grid(row = 4, column = 0, pady = 2)

#out text
output_text=tk.Text(root, height=10,width=100,bd=5)
output_text.grid(row = 6, column = 0,  pady = 2)
output_text.insert(tk.END,getText())
root.mainloop()

Upvotes: 0

Views: 414

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

What you are looking for is the columnspan option of grid(). First start off by removing custom geometry to your window. After that place the widgets all in same row but different column, like:

btnRead=tk.Button(root, height=1, width=20, text="ثبت",relief='flat',overrelief='groove', command=getText)
btnRead.grid(row=2, column=0, pady=2)

btnClear=tk.Button(root, height=1, width=20, text="پاک کردن",relief='flat',overrelief='groove',command=textClear)
btnClear.grid(row=2, column=1, pady=2)

btnFind =tk.Button(root, height=1, width=20,text = "پیدا کردن فایل",relief='flat',overrelief='groove',command =fileDialog)
btnFind.grid(row=2, column=2, pady=2)

Now add the columnspan option to both the Text widgets, like:

input_text=tk.Text(root, height=10,width=100,bd=5)
input_text.grid(row=1, column=0, pady=2, columnspan=3)

output_text=tk.Text(root, height=10,width=100,bd=5)
output_text.grid(row=6, column=0, pady=2, columnspan=3) 

What columnspan=n does is, it will make room to span n columns for the entire width of the single column where the configured widget exists. If you are used to excel, you can imagine this quite easily, like this:

                                          Excel screenshot

Upvotes: 1

Related Questions