Kenny Barber
Kenny Barber

Reputation: 245

Keeping a constant window size with WM WINDOWS in tcl

Morning all,

I have a tcl/tk application and for the windows size I have the following code:

wm title . "Relay Switch Application"
wm attributes . -alpha "1" 
wm geometry . 1600x500+100+100

Within the window, there is 3 labelframes positioned at:

x1: 10 x2: 200 x3: 320

On my PC, each labelframe are side by side with about 20 pixel gaps between them. On my work laptop, the second labelframe is over lapping the first.

How do I change or add to my code so the windows contents are postioned correctly for whatever screen resolution?

Thank you in advance.

enter image description here

Upvotes: 1

Views: 378

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137587

It's a fact of GUIs that absolute positioning of windows (either within a main window with place or of the top level with wm geometry) is usually a bad idea. This is because of little things like screen pixel densities varying or font widths changing and so on, but it's just something that you have to put up with. Instead, you should write your application to use Tk's other geometry managers, as those adapt to the size of the content and the amount of overall screen space that the OS and user conspire to give you.

For example, here's how to put three labelframes side-by-side with pack:

# Make some labelframes
labelframe .x1 -text "This is X1"
labelframe .x2 -text "This is X2"
labelframe .x3 -text "This is X3"

# Lay them out
pack .x1 .x2 .x3 -side left -fill both -expand yes
# You might want to experiment with the -padx and -pady options

If you need complex placement rules, such as stacking in several directions or making one window a size that is a multiple of the size of another, you'll probably use grid, which is a geometry manager that is sophisticated enough to handle almost all layouts you can conceive of in normal GUIs. It'd be fairly simple for this case:

# Put the labelframes in the grid…
grid .x1 .x2 .x3 -sticky nsew

# … and define how the system expands
grid columnconfigure {0 1 2} -weight 1
grid rowconfigure 0 -weight 1

Upvotes: 1

Related Questions