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

Vincent visualizations are not displaying from the command line

python python-2.7 data-visualization

I'm new to Python visualizations, and have been trying out vincent's Quick Start examples in iPython Notebook.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.