如何在Tkinter中将框架居中放置在框架中? [英] How do I center a frame within a frame in Tkinter?

查看:428
本文介绍了如何在Tkinter中将框架居中放置在框架中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个框架,使用 grid_propagate()方法固定了该框架的大小.我想将一个框架居中放置在该框架中.我该怎么办?

I have a frame that I fixed the size of using the grid_propagate() method. I'd like to center a frame within this frame. How do I go about this?

推荐答案

将其打包以填充所有方向.根据需要添加填充.

pack it to fill in all directions. Add padding as needed.

或者,使用允许您使用相对或绝对定位的位置.您可以使用.5/.5的相对x/y和"c"(中心)的锚点.

Or, use place which lets you use relative or absolute positioning. You can use a relative x/y of .5/.5 and an anchor of "c" (center).

import Tkinter as tk

root=tk.Tk()
f1 = tk.Frame(width=200, height=200, background="red")
f2 = tk.Frame(width=100, height=100, background="blue")

f1.pack(fill="both", expand=True, padx=20, pady=20)
f2.place(in_=f1, anchor="c", relx=.5, rely=.5)

root.mainloop()

这篇关于如何在Tkinter中将框架居中放置在框架中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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