Ruby/Tk/Layout

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Grid layout manager places its child widgets in a table-like arrangement

require "tk"
root = TkRoot.new
3.times { |r|
  4.times { |c|
    TkLabel.new(root) {
      text "row #{r}, column #{c}"
    }.grid("row" => r, "column" => c, "padx" => 10, "pady" => 10)
  }
}
Tk.mainloop



Layout a label

require "tk"
root = TkRoot.new() { title "Today"s Date" }
str = Time.now.strftime("Today is \n%B %d, %Y")
lab = TkLabel.new(root) do
        text str
        pack("padx" => 15, "pady" => 10,
             "side" => "top")
      end
Tk.mainloop



Pack option

require "tk"
$top  = { "side" => "top", "padx"=>5, "pady"=>5 }
$left = { "side" => "left", "padx"=>5, "pady"=>5 }
$bottom = { "side" => "bottom", "padx"=>5, "pady"=>5 }
$temp = 74   # Starting temperature...
root = TkRoot.new { title "Thermostat" }
top = TkFrame.new(root) { background "#606060" }
bottom = TkFrame.new(root)
$tlab = TkLabel.new(top) do
  text $temp.to_s
  font "{Arial} 54 {bold}"
  foreground "green"
  background "#606060"
  pack $left
end
TkLabel.new(top) do         # the "degree" symbol
  text "o"
  font "{Arial} 14 {bold}"
  foreground "green"
  background "#606060"
  # Add anchor-north to the hash (make a superscript)
  pack $left.update({ "anchor" => "n" })
end
TkButton.new(bottom) do
  text " Up "
  command proc { $tlab.configure("text"=>($temp+=1).to_s) }
  pack $left
end
TkButton.new(bottom) do
  text "Down"
  command proc { $tlab.configure("text"=>($temp-=1).to_s) }
  pack $left
end
top.pack $top
bottom.pack $bottom
Tk.mainloop



Place geometry manager

require "tk"
top = TkRoot.new {title "Label and Entry Widget"}
lb1=TkLabel.new(top){
    text "Hello World"
    background "yellow"
    foreground "blue"
    place("relx"=>0.0,"rely"=>0.0)
}
e1 = TkEntry.new(top){
    background "red"
    foreground "blue"
    place("relx"=>0.4,"rely"=>0.0)
}
Tk.mainloop



To understand the use of row and column options

require "tk"
top = TkRoot.new {title "Label and Entry Widget"}
lb1=TkLabel.new(top){
      text "Hello World"
      background "yellow"
      foreground "blue"
      grid("row"=>0, "column"=>0)
}
e1 = TkEntry.new(top){
      background "red"
      foreground "blue"
      grid("row"=>0, "column"=>1)
}
Tk.mainloop