1

I was using tkinter to make simple GUI and I wanted to make it too simple and cool so I wanted to this label ********** to be filled in screen but the there is no option for fill in grid like in pack so I am looking for alternative.

Here is the code:

import tkinter
import os


def submit():
    os.chdir("/home/samip/forms")
    with open(f'{user_val.get()}.txt', 'w') as file:
        file.write(f'''Person Details
        User: {user_val.get()}
        Age: {age_val.get()}
        Address: {address_val.get()}
        ''')
        quit()




root = tkinter.Tk()
root.geometry("400x200")
root.maxsize(400, 200)
root.minsize(400, 200)
root.title("Mars form")

lab = tkinter.Label(text="Mars Form", font=("Arial", 15, "bold"), bg="orange", fg="white", padx=10)
lab.grid()

user = tkinter.Label(text="Name:")
age = tkinter.Label(text="Age:")
address = tkinter.Label(text="Address:")

user.grid()
age.grid()
address.grid()
user_val = tkinter.StringVar()
age_val = tkinter.StringVar()
address_val = tkinter.StringVar()

user_val_entry = tkinter.Entry(root, textvariable=user_val)
age_val_entry = tkinter.Entry(root, textvariable=age_val)
address_val_entry = tkinter.Entry(root, textvariable=address_val)
user_val_entry.grid(row=1, column=1)
age_val_entry.grid(row=2, column=1)
address_val_entry.grid(row=3, column=1)


tkinter.Button(root, text="Submit", command=submit).grid(row=4, column=1)

root.mainloop()
 

The label i am trying to fill is in the 24 line.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Have you tried `width` in the config of `Entry`? – jizhihaoSAMA Jul 21 '20 at 12:50
  • I think this needs clarification since the option's name is a bit misleading. `width` differs for a `Label` and a `Button`, and an `Entry`. for the former 2 it sets the width as the **number of pixels, centimeters, inches, points** _if_ `Label` or `Button` **contain an image** of some sort, otherwise it sets the width **as the number of characters**. By contrast `Entry` _only_ sets the width as the **number of characters**. In either cases setting `width` as `< 0` makes the widget shrink to the smallest size to contain the content. – Space Jul 21 '20 at 13:38

1 Answers1

4

You're searching for columnspan and sticky:

import tkinter
import os


def submit():
    os.chdir("/home/samip/forms")
    with open(f'{user_val.get()}.txt', 'w') as file:
        file.write(f'''Person Details
        User: {user_val.get()}
        Age: {age_val.get()}
        Address: {address_val.get()}
        ''')
        quit()




root = tkinter.Tk()
root.geometry("400x200")
root.maxsize(400, 200)
root.minsize(400, 200)
root.title("Mars form")

lab = tkinter.Label(text="Mars Form", font=("Arial", 15, "bold"), bg="orange", fg="white", padx=10)
lab.grid(columnspan=2, sticky='ew')

user = tkinter.Label(text="Name:")
age = tkinter.Label(text="Age:")
address = tkinter.Label(text="Address:")

user.grid()
age.grid()
address.grid()
user_val = tkinter.StringVar()
age_val = tkinter.StringVar()
address_val = tkinter.StringVar()

user_val_entry = tkinter.Entry(root, textvariable=user_val)
age_val_entry = tkinter.Entry(root, textvariable=age_val)
address_val_entry = tkinter.Entry(root, textvariable=address_val)
user_val_entry.grid(row=1, column=1)
age_val_entry.grid(row=2, column=1)
address_val_entry.grid(row=3, column=1)


tkinter.Button(root, text="Submit", command=submit).grid(row=4, column=1)

root.mainloop()

In this line(25) here:

lab.grid(columnspan=2, sticky='ew')

I used columnspan that tells tkinter to merge columns in given number. The sticky tells tkinter that this widget sticks to 'ew' east and west. You will find code with sticky='nsew' witch means north, south, east and west.

And out of the docs:

-columnspan n Insert the slave so that it occupies n columns in the grid. The default is one column, unless the window name is followed by a -, in which case the columnspan is incremented once for each immediately following -.

-sticky style If a slave's cell is larger than its requested dimensions, this option may be used to position (or stretch) the slave within its cell. Style is a string that contains zero or more of the characters n, s, e or w. The string can optionally contains spaces or commas, but they are ignored. Each letter refers to a side (north, south, east, or west) that the slave will “stick” to. If both n and s (or e and w) are specified, the slave will be stretched to fill the entire height (or width) of its cavity. The -sticky option subsumes the combination of -anchor and -fill that is used by pack. The default is “”, which causes the slave to be centered in its cavity, at its requested size.

Update

Your follw quesion is:

It worked to some extend but I was look for alternative for fill the columnspan only takes effect with 2 I tried other values but others have no effect

Based on your Code you the best option is use the grid_columnconfigure(*column, *weight) method of tkinter. A perfectly fine explaination can be found here.

Why is this the best choice in your case ? Just because you used the geometry method of tkinter. So how does it work?

tkinter calculate the size of the window by itself if nothing is predefined by the developer. Therefore you could also just delete your lines of the geometry method to have a simliar effect.

But since you had predifned the geometry you had built space. The grid method of tkinter also calculate the size of a column or a row automatically by the biggest widget it contains. So if the biggest widget you have is configured with (width=100) and the width is defined in pixel you would get each column with a width of 100 pixel. Same as row with height.

That's why you need to use the grid_columnconfigure method of tkinter. In short this method tells tkinter which widget have wich priority of getting space, if there is empty space.

A full exampel can be found below I edited:

  1. the root columnconfigure.
  2. bound your button to a variable.
  3. seperated the grid method form the variable. Because every method returns something. more
  4. edited the grid parameter to center it with (column=0, columnspan=2)

import tkinter
import os

  
def submit():
    os.chdir("/home/samip/forms")
    with open(f'{user_val.get()}.txt', 'w') as file:
        file.write(f'''Person Details
        User: {user_val.get()}
        Age: {age_val.get()}
        Address: {address_val.get()}
        ''')
        quit()




root = tkinter.Tk()
root.geometry("400x200")
root.maxsize(400, 200)
root.minsize(400, 200)
root.title("Mars form")

lab = tkinter.Label(text="Mars Form", font=("Arial", 15, "bold"), bg="orange", fg="white", padx=10)
lab.grid(columnspan=2, sticky='ew')
root.columnconfigure(0, weight=1)

user = tkinter.Label(text="Name:")
age = tkinter.Label(text="Age:")
address = tkinter.Label(text="Address:")

user.grid()
age.grid()
address.grid()
user_val = tkinter.StringVar()
age_val = tkinter.StringVar()
address_val = tkinter.StringVar()

user_val_entry = tkinter.Entry(root, textvariable=user_val)
age_val_entry = tkinter.Entry(root, textvariable=age_val)
address_val_entry = tkinter.Entry(root, textvariable=address_val)
user_val_entry.grid(row=1, column=1)
age_val_entry.grid(row=2, column=1)
address_val_entry.grid(row=3, column=1)


b1 = tkinter.Button(root, text="Submit", command=submit)
b1.grid(row=4, ,column=0, columnspan=2)

root.mainloop()

Now you should know all the basic information of tkinter grid method to be abel to work your GUI out. Have fun!

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • It worked to some extend but i was look for alternative for ```fill``` the ```columnspan``` only takes effect with 2 i tried other values but others have no effect please help me. –  Jul 23 '20 at 02:39
  • @SamipKarki I updated my answer for you. I would appreciate if you accept my answer. – Thingamabobs Jul 23 '20 at 14:02