窗口中框架中的标签不会拉伸,为什么? [英] A Label in a Frame in a window won't stretch, why?

查看:42
本文介绍了窗口中框架中的标签不会拉伸,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展示

  • 一个窗口
  • 使用单个 Frame
  • Frame 中的 Label 将拉伸到窗口的整个宽度
  • a window
  • with a single Frame
  • and a Label in the Frame which would stretch to the whole width of the window

以下代码

import Tkinter as tk

root = tk.Tk()
root.geometry("100x100")
# first column of root will stretch
root.columnconfigure(0, weight=1)

# a frame in root
upper_frame = tk.Frame(root)
# first column of upper_frame will stretch
upper_frame.columnconfigure(0, weight=1)
upper_frame.grid(row=0, column=0)

# a label in upper_frame, which should stretch
mylabel = tk.Label(upper_frame)
mylabel.grid(row=0, column=0)
mylabel.configure(text="hello", background="blue")

root.mainloop()

显示

为什么 Label 没有拉伸到窗口的整个宽度,而是和文本一样宽?

Why isn't the Label stretching to the whole width of the window but is just as wide as the text?

推荐答案

在调用 grid(e = 东,w = 西).否则单元格中的小部件居中对齐.

Specifying sticky option when you call grid (e = east, w = west). Otherwise the widget in the cell is center-aligned.

upper_frame.grid(row=0, column=0, sticky='ew')
..
mylabel.grid(row=0, column=0, sticky='ew')

这篇关于窗口中框架中的标签不会拉伸,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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