简体   繁体   English

tkinter 框架内的网格?

[英]Grid within frame in tkinter?

I am trying to build a grid structure in python tinker that is positioned aside each other without any free space in between, but I am somehow not able to achieve this.我正在尝试在 python tinker 中构建一个网格结构,该网格结构彼此并排放置,中间没有任何可用空间,但我不知何故无法实现这一点。

Could anyone help in what am I doing wrong?任何人都可以帮助我做错什么吗?

Here is the code.这是代码。

import tkinter as tk


window = tk.Tk()

window.geometry("700x600")
frame1=tk.Frame(window,width=350,height=400,bg='black')
frame1.grid(row=0,column=0,rowspan=1,columnspan=1)


frame2=tk.Frame(window,width=350,height=600,bg='white')
frame2.grid(row=0,column=1)

frame3=tk.Frame(window,width=350,height=200,bg='red')
frame3.grid(row=1,column=0,rowspan=1,columnspan=1)


window.mainloop()

在此处输入图片说明

Here is the Output that I am getting, though what I want is that the black frame and red frame fit together without any gap in between, so that the overall height remains 600.这是我得到的输出,虽然我想要的是黑框和红框之间没有任何间隙地组合在一起,以便总高度保持 600。

The problem is because frame2 in the second column is taller than frame1 to the left of it.问题是,因为frame2在第二列比更高的frame1到它的左侧。 To workaround that, simply specify that it spans both rows that frame1 and frame3 occupy.要解决这个问题,只需指定它跨越frame1frame3占据的两行。

import tkinter as tk


window = tk.Tk()
window.geometry("700x600")

frame1=tk.Frame(window, width=350, height=400, bg='black')
frame1.grid(row=0, column=0, rowspan=1, columnspan=1)

frame2=tk.Frame(window, width=350, height=600, bg='white')
frame2.grid(row=0, column=1, rowspan=2)  # Added rowspan here.

frame3=tk.Frame(window, width=350, height=200, bg='red')
frame3.grid(row=1, column=0, rowspan=1, columnspan=1)

window.mainloop()

Result:结果:

结果截图

The problem is in the rowspan argument in frame2 .问题是在rowspan的说法frame2

import tkinter as tk


window = tk.Tk()

window.geometry("700x600")
frame1 = tk.Frame(window, width=350, height=400, bg='black')
frame1.grid(row=0, column=0, rowspan=1, columnspan=1)

frame2 = tk.Frame(window, width=350, height=600, bg='white')
frame2.grid(row=0, column=1, rowspan=2) # here add rowspan = 2 argument

frame3 = tk.Frame(window, width=350, height=200, bg='red')
frame3.grid(row=1, column=0, rowspan=1, columnspan=1)


window.mainloop()

This is happening because you are having 2 frames on the left side both taking one row itself (2 rows total), and you need to span the right white frame to 2 rows to get the grid layout you want (to make them equal on right and left), otherwise it gets messed up because of difference between the number of frames in column 0 (2) and number of frames in column 1 (1).发生这种情况是因为您在左侧有 2 个框架,它们各自占据一行(总共 2 行),并且您需要将右侧的白色框架跨越到 2 行以获得您想要的网格布局(使它们在右侧相等和左),否则它会因为第 0 (2) 列中的帧数和第 1 (1) 列中的帧数之间的差异而变得混乱。

更改代码后得到的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM