python - 窗口框架中的标签不会拉伸(stretch),为什么?

标签 python python-2.7 tkinter

我想显示

  • 一扇 window
  • 使用单个框架
  • Frame 中的 Label 将拉伸(stretch)到窗口的整个宽度

以下代码

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()

显示

enter image description here

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

最佳答案

在调用grid时指定粘性选项(e = 东,w = 西)。否则单元格中的小部件将居中对齐。

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

enter image description here

关于python - 窗口框架中的标签不会拉伸(stretch),为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596354/

相关文章:

python - 在 PySimpleGUI 窗口中插入图像作为背景

python - 按python中的值对字典进行排序

python - 在另一个脚本终止后运行 python 脚本

python - 使用 EBO 时 OpenGL 崩溃

Python3.4 : Opening file with mode 'w' still gives me FileNotFound error

python - Scrapy如何在同一个进程中运行多个爬虫

python - 如何执行另一个python文件然后关闭现有的文件?

python - 如何在 tkinter 中向从 csv 读取的表格的每一行添加复选按钮?

python - Tkinter Canvas 和滚动条

python-2.7 - sudo pip install 的权限警告