Prevent Label to be re-aligned after askdirectory() is closed

I built a window which display some labels, an entry field and a button. The button opens a tkinter.filedialog.askdirectory(). After I choose a directory and close the dialog another label is updated along with the text entry with the choice.

The problem is that after this happens the description of the text entry (another Label on its left) is realigned in its cell. I don't know why this happens, is there any behind-the-scenes behaviour which I'm not aware of?

Here's the sample code I use.

from tkinter import *
from tkinter import filedialog
from tkinter import ttk


def folderDiag():
    scanfolder.set(filedialog.askdirectory())

c = (N, W, S, E)
scanfolder = StringVar()

root = Tk()
root.title("Testing")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

frame = ttk.Frame(root, padding="3")
frame.grid(column=0, row=0, sticky=c)
frame.columnconfigure(0, weight=1)
frame.columnconfigure(1, weight=1)
frame.columnconfigure(2, weight=1)
frame.rowconfigure(0, weight=1)
frame.rowconfigure(1, weight=1)
frame.rowconfigure(2, weight=1)
ttk.Label(frame, text="Welcome!",
          font="TkDefaultFont 24 bold").grid(column=0, row=0, columnspan=4,
                                             sticky=c)
# This label will be centered after dialog is closed
ttk.Label(frame, text="Select folder to scan:").grid(column=0, row=1, sticky=E)
ttk.Label(frame, textvariable=scanfolder).grid(column=2, row=2)

scan_folder_entry = ttk.Entry(frame, textvariable=scanfolder).grid(column=3,
                                                                   row=1)
browse_button = ttk.Button(frame, text="Browse...",
                           command=folderDiag).grid(column=4, row=1)

root.mainloop()
exit(1)

Answers


if I understand correctly, you need this:

|    0    |         1         |      2     |  # Column number
|                Welcome!                  |
| Select  |       Entry       |   Button   |
| Your label                               |

So:

from tkinter import *
from tkinter import filedialog
from tkinter import ttk

def folderDiag():
    scanfolder.set(filedialog.askdirectory())

root = Tk()

c = (N, W, S, E)
scanfolder = StringVar()

root.title("Testing")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

frame = ttk.Frame(root, padding="3")
frame.grid(column=0, row=0, sticky=c)
frame.columnconfigure(0, weight=0)
frame.columnconfigure(1, weight=1)
frame.columnconfigure(2, weight=0)
frame.rowconfigure(0, weight=1)
frame.rowconfigure(1, weight=1)
frame.rowconfigure(2, weight=1)
ttk.Label(frame, text="Welcome!",
          font="TkDefaultFont 24 bold").grid(column=0, row=0, columnspan=3,
                                             sticky=c)
ttk.Label(frame, text="Select folder to scan:").grid(column=0, row=1, sticky=E)
ttk.Label(frame, textvariable=scanfolder, justify=LEFT).grid(column=0, columnspan=3, row=2, sticky=W)

scan_folder_entry = ttk.Entry(frame, textvariable=scanfolder)
scan_folder_entry.grid(column=1, row=1, sticky=EW)
browse_button = ttk.Button(frame, text="Browse...",
                           command=folderDiag)
browse_button.grid(column=2, row=1)

root.mainloop()

Your code create this layout:

|   0    | 1 |    2    |    3   |    4   |
| Welcome                       |  ---   |
| Select | - |   ---   | Entry  | Button |
|   ---  | - |  Label  |   ---  |   ---  |

For some reason you leave #1 empty. Its width = const = 0. Forget about it. Select doesn't attached to Entry. Columns #1 and #2 separate them. Select attached to right edge of #0 column. When you update Label you increase width of #2 column => you increase space between #0 (Select) and #3 (Entry). Select stay attached to right edge of #0.


Need Your Help

Changing View Controllers - SIGABRT

objective-c xcode uiviewcontroller sigabrt

I am trying to move from one view controller to another when a button is pressed, but it is giving me "sigabrt" error.

JQuery and Javascript doesn't work in my project, but on web does

javascript jquery html asp.net-mvc-3 jquery-ui

I reused a code that I saw on StackOverFlow with my data : http://jsfiddle.net/ctx3H/91/ , but when I'm importing him in Visual Studio 10 the buttons doesn't work.