1

This is my sample code:

from tkinter import *
from tkinter import ttk, scrolledtext

class MainWindow:
    def __init__(self):
        self.parent=Tk()
        self.parent.geometry("500x300+370+100")
        self.parent.title("My Software")
        
        self.Frame1=Frame(self.parent, background="#ffffff")
        self.Frame1.place(x=10, y=10, width=400, height=250)

        self.style=ttk.Style()
        self.style.configure("InWhiteFrame.TButton", background="#ffffff", padding=0) # it makes "invisible" the extra 1px border in the ttk buttons.
        
        self.Frame1.grid_columnconfigure(0, minsize=16)
        self.Frame1.grid_columnconfigure(4, weight=1) # it adapts the Entry1 widgets to the available size.
        self.Frame1.grid_columnconfigure(1, minsize=1) # 1px column to "delete" the extra 1px border from the ttk buttons (see the "test" button)
        self.Frame1.grid_columnconfigure(3, minsize=10)
        self.Frame1.grid_columnconfigure(5, minsize=8)
        self.Frame1.grid_columnconfigure(7, minsize=1, pad=1)
        self.Frame1.grid_columnconfigure(8, minsize=34, pad=34)  # 1px column to "delete" the extra 1px border from the ttk buttons (see the "..." button)
        self.Frame1.grid_rowconfigure(0, minsize=16, pad=16)
        self.Frame1.grid_rowconfigure(2, minsize=12, pad=12)
        self.Frame1.grid_rowconfigure(4, minsize=12, pad=12)
        self.Frame1.grid_rowconfigure(6, minsize=30, pad=30)

        ttk.Label(self.Frame1, text="bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla:", wraplength=514, background="#ffffff").grid(row=1, column=2, columnspan=7, sticky="w")

        self.MyLabel=ttk.Label(self.Frame1, text="bla bla bla:", background="#ffffff")
        self.MyLabel.grid(row=3, column=2, sticky="w")
        
        self.Entry1=ttk.Entry(self.Frame1)
        self.Entry1.grid(row=3, column=4, sticky="ew")
        
        self.Button1=ttk.Button(self.Frame1, text="...", width=4, style="InWhiteFrame.TButton")
        self.Button1.grid(row=3, column=6, columnspan=7, sticky="w")

        self.TextBox=scrolledtext.ScrolledText(self.Frame1, wrap=WORD, height=6, font=("Segoe UI", 9))
        self.TextBox.grid(row=5, column=2, columnspan=6, sticky="we")

        self.Button2 = ttk.Button(self.Frame1, text="test", style="InWhiteFrame.TButton", command=self.ChangeMyLabel)
        self.Button2.grid(row=7, column=1, columnspan=7, sticky="w")
        
        self.parent.mainloop()
    
    def ChangeMyLabel(self):
        self.MyLabel.configure(text="bla bla bla bla bla bla:")
        
    

def main():
    app=MainWindow()

if __name__=="__main__":
    main()

in my frame (Frame1) I chosed to place the ttk widgets using the grid method. I configured eight columns. the first and the last two ones are really important for the design. the first one creates an extra space, and the second one helps to "hide" the extra border around the ttk button (Button2). see the below image to understand exactly what I mean:

enter image description here

Without the second column (composed by just one pixel) the ttk button (Button2) would be not align with the ScrolledText widget. it just a workaround to "delete" the annoying extra border placed around the ttk buttons. I tried to delete it with some options from the "Button2" but no ones solved the issue. I opened also a topic here, but unfortunately nobody know how to delete the extra border from the ttk buttons, probably because it's not possible.. I don't know.

Tkinter - How can I remove the background borders from ttk.Button?

This part of my code works perfectly but I can't say the same for the code that regards the last two columns. they are very similar to the two first ones. the eighth column should to crate an extra space (34px), and the seventh one (1px) should hide the extra border from the ttk button (Button1) but they doesn't work. see the image below:

enter image description here

As you can see, the widgets placed in the third row, composed by a label, an entry and a button, are not aligned to the ScrolledText widget.

My question is: how can I place the widgets in my frame (Frame1) in order to "hide" the extra border from the ttk buttons, and at the same time align all of them correctly as shown in the screenshot below?

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TurboC
  • 777
  • 1
  • 5
  • 19
  • Why does `Button1` have a columnspan of 6? That extends it into columns that are to the right of the columns occupied by the ScrolledText. – jasonharper Oct 22 '20 at 21:12
  • @jasonharper: you are right, I just changed it into `columnspan=7`, but it doesn't solve the isse. probably it was just an error from my hundreds and hundreds of attempts to solve the allign issue.. – TurboC Oct 22 '20 at 21:18
  • `columnspan=7` just pushes it even further to the right of the ScrolledText. I don't see any obvious reason why it needs a columnspan at all. – jasonharper Oct 22 '20 at 22:01
  • @jasonharper: no, the `columnspan=7` puts the Button1 on both colum 6 and 7. as I already wrote, it helps me to hide the extra border from the ttk button. I implemented succesfully the same concept to the Button2 using the second column. but anyway.. why my eighth column, composed by 34 pixels, doesn't appear with 34 pixels in my frame? why doesn't it "push" the Button1 widget to left side? please if it's simple, fix my code, I'm becoming crazy.. I really don't know how to solve my alignment issue. – TurboC Oct 22 '20 at 22:12
  • 1
    `columnspan` is *the number of columns occupied*, default 1. You're putting that Button all the way from columns 6 to 12. – jasonharper Oct 22 '20 at 22:20
  • 1
    You can add `padx=1` to `self.textbox.grid(...)` instead of using 1-pixel empty columns. – acw1668 Oct 23 '20 at 01:43
  • You could be intrested in https://stackoverflow.com/questions/63536505/how-do-i-organize-my-tkinter-appllication/63536506#63536506 – Thingamabobs Oct 23 '20 at 10:46
  • @jasonharper: thanks! I thought that the `columnspan` option meant where to expand the column, .. it was my fault. now I fixed my code correcty! I followed the advice from acw1668 too. – TurboC Oct 23 '20 at 15:09
  • acw1668: thanks! your trick is better ;) – TurboC Oct 23 '20 at 15:10
  • You can just use 4 rows x 3 columns grid. – acw1668 Oct 23 '20 at 16:08

0 Answers0