框架内的标签(Tkinter) [英] Label within a Frame (Tkinter)

查看:37
本文介绍了框架内的标签(Tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里挠头.我是 Tkinter 的新手.我试图找出一些基本的东西,比如在框架内放置标签.我遇到的问题是当显示标签时,它不会继承父级的大小.事实上,它实际上改变了放置它的框架的大小.我究竟做错了什么?最后,我想在不同的框架中添加更多标签和按钮,而不是在其中添加 BG 颜色.

I'm scratching me head here. I'm very new at Tkinter. I'm trying to figure out something as basic as placing a label within a frame. The problem I'm having is when the label is displayed, it doesn't inherit the size of the parent. In fact, it actually changes the size of the frame in which it is placed. what am I doing wrong? In the end I want to add more labels and buttons in the different frames without the BG color in them.

main = Tk()

main.geometry('1024x768+0+0')

fm1 = LabelFrame(main, width = 1024, height = 68, bg = 'RED')
fm1.grid(row = 0, columnspan = 2)

label = Label(fm1, text = 'test')
label.grid(row = 0, sticky = N+E+S+W)

fm2 = Frame(main, width = 1024, height = 200, bg = 'BLUE')
fm2.grid(row = 1, columnspan = 2)

fm3 = Frame(main, width = 512, height = 300, bg = 'GREEN')
fm3.grid(row = 2, column = 0)

fm4 = Frame(main, width = 512, height = 300, bg = 'BLACK')
fm4.grid(row = 2, column = 1)

fm5 = Frame(main, width = 1024, height = 200, bg = 'YELLOW')
fm5.grid(row = 3, columnspan = 2)

推荐答案

根据我的经验,如果您在主窗口中放置多个框架并希望它们填充放置它们的行/列,则需要配置使用 grid_rowconfiguregrid_columnconfigure 设置主窗口的行/列并给它们一个权重.

From my experience, if you are placing multiple frames within your main window and want them to fill the rows / columns where you put them, you need to configure the rows/columns of your main window using grid_rowconfigure and grid_columnconfigure and give them a weight.

我已经重新格式化了上面的示例,以包含配置行来展示我如何使用它们.

I have re-formatted your example above to include the configuration lines to show how I use them.

main = Tk()
main.geometry('1024x768+0+0')

# Congigure the rows that are in use to have weight #
main.grid_rowconfigure(0, weight = 1)
main.grid_rowconfigure(1, weight = 1)
main.grid_rowconfigure(2, weight = 1)
main.grid_rowconfigure(3, weight = 1)

# Congigure the cols that are in use to have weight #
main.grid_columnconfigure(0, weight = 1)
main.grid_columnconfigure(1, weight = 1)


# Define Frames - use sticky to fill allocated row / column #
fm1 = Frame(main, bg = 'RED')
fm1.grid(row = 0, columnspan = 2, sticky='news')

fm2 = Frame(main, bg = 'BLUE')
fm2.grid(row = 1, columnspan = 2, sticky='news')

fm3 = Frame(main, bg = 'GREEN')
fm3.grid(row = 2, column = 0, sticky='news')

fm4 = Frame(main, bg = 'BLACK')
fm4.grid(row = 2, column = 1, sticky='news')

fm5 = Frame(main, bg = 'YELLOW')
fm5.grid(row = 3, columnspan = 2, sticky='news')

# Notice 'pack' label - fine to use here as there are no conflicting grid widgets in this frame #
lab = Label(fm1, text = 'TEST LABEL')
lab.pack()

main.mainloop()

PS - 我已经删除了你指定的高度和宽度 - 这不是必需的,因为你告诉框架填充它被网格化"的配置行/列中的所有空间.

PS - I have removed your specified heights and widths - this is not necessary as you are telling the frame to fill all the space in the configured row / column in which it is 'gridded'.

最后,您的第一帧被定义为LabelFrame",我不确定在此示例中是否有必要.

Lastly, your first frame was defined as a 'LabelFrame' which im not fully sure is necessary in this example.

希望这有帮助!卢克

这篇关于框架内的标签(Tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆