2

I'm a python newbie but playing with tkinter. I have created a notebook in a window with several tabs. I am trying to place a widget on a tab, which I can do but I can't position it, it just keeps going to the top right hand side of the tab. I am trying to use the grid rows and columns but it seems to ignore it. Sorry if this is a basic question!

Thanks in advance

from tkinter import *
from tkinter import ttk


main = Tk()
main.title("Deploy")
main.geometry("1250x900")

rows = 0
while rows < 50:
    main.rowconfigure(rows,weight=1)
    main.columnconfigure(rows,weight=1)
    rows += 1


nb = ttk.Notebook(main)
nb.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='NESW')

page1=ttk.Frame(nb)
nb.add(page1,text='Tab1')
page2=ttk.Frame(nb)
nb.add(page2,text='Tab2')
page3=ttk.Frame(nb)
nb.add(page3,text='Tab3',state="disabled")
page4=ttk.Frame(nb)
nb.add(page4,text='Tab4',state="disabled")
page5=ttk.Frame(nb)
nb.add(page5,text='tab5',state="disabled")

labelL1 = ttk.Label(page1, text="User name:")
labelL1.grid(row=10, column=10,sticky='WE', columnspan=3)


main.mainloop()
Daves
  • 141
  • 1
  • 3
  • 6
  • the `.grid()` geometry manager does not work as you seem to be assuming. you need to take a look at the [grid geometry](http://effbot.org/tkinterbook/grid.htm) docs – Samuel Kazeem Feb 16 '19 at 10:41

1 Answers1

2

You want to position a widget at row 10, column 10 in a grid but the issue here is that since the preceding columns and rows in the grid are empty, they don't take up any space.

To achieve the positioning one could give rows and columns a minimum size, so that they take up space even if empty like this:

from tkinter import *
from tkinter import ttk


main = Tk()
main.title("Deploy")
main.geometry("1250x900")

rows = 0
while rows < 50:
    main.rowconfigure(rows, weight=1)
    main.columnconfigure(rows, weight=1)
    rows += 1


nb = ttk.Notebook(main)
nb.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='NESW')

page1=ttk.Frame(nb)
nb.add(page1, text='Tab1')
page2=ttk.Frame(nb)
nb.add(page2, text='Tab2')
page3=ttk.Frame(nb)
nb.add(page3, text='Tab3', state="disabled")
page4=ttk.Frame(nb)
nb.add(page4, text='Tab4', state="disabled")
page5=ttk.Frame(nb)
nb.add(page5, text='tab5', state="disabled")

labelL1 = ttk.Label(page1, text="User name:")

# add this to give a min size to empty rows
rows = 0
while rows < 10:
    page1.rowconfigure(rows,weight=1, minsize=1)
    page1.columnconfigure(rows, weight=1, minsize=1)
    #ttk.Label(page1, text="-").grid(row=rows, column=0)
    rows += 1

labelL1.grid(row=10, column=10, sticky='WE', columnspan=3)


main.mainloop()

But perhaps another solution would be more in line with the grid concept, for instance using padx, padx options

A similar question: Tkinter.grid spacing options?

user2314737
  • 27,088
  • 20
  • 102
  • 114