tkinter - 为什么 tkinter 框架与使用网格方法的 Tk 窗口大小不同?
问题描述
我可能在这里很昏暗,但我不明白为什么在调整窗口大小和使用 Grid 方法时ttk.Frame
,这个简单脚本中的小部件没有随窗口一起增长?Tk
我不希望ttk.Label
默认情况下带有图像的窗口会调整大小。但是,当窗口大于给定方法rowconfigure
并columnconfigure
使用的标签时,我希望在标签周围看到粉红色(框架的背景颜色)。我的错误在哪里?
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
IMAGE = './images/test.jpg'
class App( ttk.Frame ):
def __init__( self, master, **kwargs ):
self.master = master
super().__init__( master, style='app.TFrame', **kwargs )
photo = ImageTk.PhotoImage( Image.open( IMAGE ) )
self.screen = ttk.Label( self, image=photo, style='app.TLabel' )
self.screen.image = photo
self.screen.grid( row=0, column=0, sticky='nsew' )
def init_ttkStyle():
'''Initialize ttk widget styles.'''
s = ttk.Style()
s.configure( 'app.TFrame', background='pink' )
s.configure( 'app.TLabel', background='cyan', borderwidth=10, relief='ridge' )
return s
def main():
ROOT = tk.Tk()
appStyle = init_ttkStyle()
app = App( ROOT )
app.grid( row=0, column=0, sticky='nsew' ) # ?
app.columnconfigure( 0, weight=1 ) # ?
app.rowconfigure( 0, weight=1 ) # ?
#app.pack( expand=1, fill='both' ) #this works
ROOT.mainloop()
if __name__ == "__main__":
main()
解决方案
app
是一个Frame
小部件。当您在小部件上使用rowconfigure
和时,它将按照您的规范进行网格化,但窗口不会。它适用于 的情况,因为它已被明确告知和完整的窗口。columnconfigure
Frame
root
.pack
expand
fill
root
要使其以类似的方式工作,请执行rowconfigure
and columnconfigure
on root
。
推荐阅读
- python - 当 np.nan 存在时 Pandas cumcount()
- directx - 如何强制执行 DirectX9 而不是 DirectX11
- c# - C#.net Crystal Report 在客户端机器上运行
- node.js - 禁用http端口时如何检查RabbitMQ是否启动并运行
- sparql - 将 GATE gazzeetteer_lkb 连接到 geonames 远程存储库
- qt - 如何在 QML 的列表中直接声明项目?
- php - 输入值未存储在数据库中,但正在读取选项
- python - 如何使用easygui在同一窗口中添加ynbox和messagebox
- node.js - 如何在 MongoDb 中一次将数据插入多个数据库。
- sql - 将多行合并为一行