开发者

Tk, how to make a layout with .pack?

When I place two buttons in the 'top' part of a TkRoot they are below each other. What are the parameters for '.pack' having them aligned horizontal? (the solution should also app开发者_如何学Goly for n-buttons)

Ruby code would be great but anything that works with tk is great. (if you are a python etc. person)

Edit: The following thing is needed: inside 'left' is a TkText+TkScrollbar to the right of it, I can only use 'top' to be sure it works as intended (that the buttons are ABOVE the TkText)


The Tk packer works by taking a slice off the remaining space inside the outer widget and allocating that slice to the inner widget. (The side says which side of the space the slice is taken off, fill says how the inner widget occupies the space, and expand says how things respond when the size of the outer widget changes.) Within the slice, the inner widget is centered. It can take some tweaking to get things right; it's usually easier if you change the widgets' backgrounds to different (garish) colors when doing this, as that makes it easier to see what's happening.

If what you want can't be done with the packer, either you need to use nested frames or (better) switch to using the gridder layout engine, which is much more powerful. (Very funky stuff can require listening to <Configure> events or using the placer, but really they're both rare things. 99.9% of all layouts can be done with pack and grid.)


Unlike many Tk developers, I prefer pack to grid, because it's easier to form a mental model of how it works. It is also possible to create quite sophisticated layouts using pack. The way I approach this conceptually is to use a frame as a container and then pack things in sequence (all right or left) or side-by-side (left/right). Using this container/child approach, you can pack things pretty much anywhere you want. Make sure the things you want to align are within the same container/frame.


You have to pack the buttons to the left. I modified a Tk example I found online:

require 'tk'

root = TkRoot.new
# button 1
btn_OK = TkButton.new(root) do
  text "OK"
  borderwidth 5
  underline 0
  state "normal"
  cursor "watch"
  font TkFont.new('times 20 bold')
  foreground  "red"
  activebackground "blue"
  relief      "groove"
  command (proc {})
  pack("side" => "left",  "padx"=> "50", "pady"=> "10")
end
# button 2
btn_Cancel = TkButton.new(root) do
  text "Cancel"
  borderwidth 5
  underline 0
  state "normal"
  cursor "watch"
  font TkFont.new('times 20 bold')
  foreground  "red"
  activebackground "blue"
  relief      "groove"
  command (proc { exit })
  pack("side" => "left",  "padx"=> "50", "pady"=> "10")
end
Tk.mainloop


Pack all by itself is not real good at placing things both horizontally and vertically in tne same container. This isn't a universal truth, but more like a rule of thumb. There are two conventional solutions: use nested frames -- one or more for items arranged horizontally and one or more for items arranged vertically -- or switch to using grid.

Note that nested frames doesn't mean you need to change the parent of existing widgets. The in option for pack (and grid and place) is extremely useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜