tkinter设置比例(创建一个2x2框)

时间:2017-09-05 15:38:03

标签: python tkinter

我想在python的tkinter中创建一个2×2的盒子,这将是我的" world"。 有没有办法在"世界"?

上设置X和Y轴

类似的东西:

  setXscale(-1.0, +1.0);
  setYscale(-1.0, +1.0);

1 个答案:

答案 0 :(得分:1)

可以使用.pack()方法完成此操作,如下所示:

from tkinter import *

root = Tk()

top = Frame(root)
bottom = Frame(root)
topleft = Frame(top)
topright = Frame(top)
bottomleft = Frame(bottom)
bottomright = Frame(bottom)

lbl1 = Label(topleft, text="topleft")
lbl2 = Label(topright, text="topright")
lbl3 = Label(bottomleft, text="bottomleft")
lbl4 = Label(bottomright, text="bottomright")

top.pack(side="top")
bottom.pack(side="bottom")
topleft.pack(side="left")
topright.pack(side="right")
bottomleft.pack(side="left")
bottomright.pack(side="right")

lbl1.pack()
lbl2.pack()
lbl3.pack()
lbl4.pack()

root.mainloop()

这会创建一个top框架和一个bottom框架,每个框架都包含一个左右框架。 然后将这些框架打包在各自的side

或者,使用.grid()可以更轻松地完成此操作:

from tkinter import *

root = Tk()

topleft = Frame(root)
topright = Frame(root)
bottomleft = Frame(root)
bottomright = Frame(root)

lbl1 = Label(topleft, text="topleft")
lbl2 = Label(topright, text="topright")
lbl3 = Label(bottomleft, text="bottomleft")
lbl4 = Label(bottomright, text="bottomright")

topleft.grid(row = 0, column = 0)
topright.grid(row = 0, column = 1)
bottomleft.grid(row = 1, column = 0)
bottomright.grid(row = 1, column = 1)

lbl1.grid(row = 0, column = 0)
lbl2.grid(row = 0, column = 0)
lbl3.grid(row = 0, column = 0)
lbl4.grid(row = 0, column = 0)

root.mainloop()

或者像这样:

from tkinter import *

root = Tk()

lbl1 = Label(root, text="topleft")
lbl2 = Label(root, text="topright")
lbl3 = Label(root, text="bottomleft")
lbl4 = Label(root, text="bottomright")

lbl1.grid(row = 0, column = 0)
lbl2.grid(row = 0, column = 1)
lbl3.grid(row = 1, column = 0)
lbl4.grid(row = 1, column = 1)

root.mainloop()