I have a tkinter pop-up with four objects and a button.
I need them to be placed in this way (showing using excel):
I am placing the labels like this:
lb1.grid(row=1,column=1)
lb3.grid(row=2,column=1)
lb3.grid(row=3,column=1,sticky='w')
lb4.grid(row=3,column=1,sticky='e')
However, when I put button using following, it doesn't stretch across all rows. Instead it stays as a row 4 item.
bt.grid(rowspan=3,column=2) #have even tried adding sticky = 'ns' to this but doesn't stretch
Basically, I want the button to be a thin vertical line on the right border of the widget. I will also want to remove the bevel of the button, text will be blank, and color will be same as label background to make it difficult to see the button. But it should be clickable across the whole east side border of the widget across all the labels.
Can we do that?
CodePudding user response:
Your problem is that you are placing the lb4 label on the lb3 label. You also should specify a columnspan = 2 for lb1 and lb2 as they span 2 columns (column 1, where lb3 sits, and column 2 where lb4 sits). bt will be in column 3 starting from row 1 with a rowspan = 3 therefor reaching till row = 3; Code:
from tkinter import *
root = Tk()
root.geometry("400x400")
lb1 = Label(root, text="Obj1")
lb2 = Label(root, text="Obj2")
lb3 = Label(root, text="Obj3")
lb4 = Label(root, text="Obj4")
lb1.grid(row=1,column=1, columnspan=2)
lb2.grid(row=2,column=1, columnspan=2)
lb3.grid(row=3,column=1,sticky='w')
lb4.grid(row=3,column=2,sticky='e')
bt = Button(root, text="Button")
bt.grid(rowspan=3, row=1, column=3, sticky="ns")
root.mainloop()
also note that rows and columns actually start at 0 (like most things in python), but because i wasn't sure if you already had some widgets in row = 0 / column = 0, i used your column numbers / row numbers.