tkinter框架无法正常工作

时间:2018-06-15 16:33:02

标签: python-3.x tkinter grid frame

任何人都可以帮助我columnspan对此代码没有任何影响:

import tkinter as tk

root = tk.Tk()

frametop = tk.Frame(root).grid(row = 0, column = 0, columnspan = 2)
labeltest1 = tk.Label(frametop, text="A").grid(row = 0, column = 0)
labeltest2 = tk.Label(frametop, text="B").grid(row = 0, column = 1)
labeltest3 = tk.Label(frametop, text="C").grid(row = 0, column = 2)

framebottom = tk.Frame(root).grid(row = 1, column = 0)
labeltest4 = tk.Label(framebottom, text="Hello World").grid(row = 1, column = 0)
labeltest5 = tk.Label(framebottom, text="Hello World").grid(row = 1, column = 1)
labeltest6 = tk.Label(framebottom, text="Hello World").grid(row = 1, column = 2)

root.mainloop()

labeltest1labeltest2labeltest3正在与framebottom的小部件类似的列中分发。我想要的是frame内有3列的网格,其中columnspan = 2具有相同的行 类似的东西:

||   A    |   B   |   C    ||  
| Hello World | Hello World | Hello World |

1 个答案:

答案 0 :(得分:1)

您的代码的组织方式,Frames无效;当您grid与创建窗口小部件位于同一行时,会返回None并将其存储在您用于FramesLabel的变量中。结果是标签直接插入root

以下内容相同,并根据您的请求放置标签。

import tkinter as tk

root = tk.Tk()

tk.Label(root, text="A").grid(row=0, column=0, columnspan=2)
tk.Label(root, text="B").grid(row=0, column=2, columnspan=2)
tk.Label(root, text="C").grid(row=0, column=4, columnspan=2)

tk.Label(root, text="Hello World").grid(row=1, column=0, columnspan=3)
tk.Label(root, text="Hello World").grid(row=1, column=3, columnspan=3)
tk.Label(root, text="Hello World").grid(row=1, column=6, columnspan=3)

root.mainloop()

GUI外观(osX):

enter image description here