Python Tkinter - 设置条目网格宽度 100%
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24945467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Tkinter - Set Entry grid width 100%
提问by Patrick Burns
When i create button + entry + button
in grid, entry
was centered but not completely fill the column. How i can fill the column via Entry
?
当我button + entry + button
在网格中创建时,entry
居中但未完全填充该列。我如何通过填写该列Entry
?
# Python 3.4.1
import io
import requests
import tkinter as tk
from PIL import Image, ImageTk
def get_image():
im = requests.get('http://lorempixel.com/' + str(random.randint(300, 400)) + '/' + str(random.randint(70, 120)) + '/')
return Image.open(io.BytesIO(im.content))
class ImageSelect(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
master.resizable(width=False, height=False)
master.title('Image manager')
master.iconify = False
master.deiconify = False
master.grab_set = True
image = ImageTk.PhotoImage(get_image())
self.image = tk.Label(image=image)
self.image.image = image
self.image.grid(row=0, columnspan=3)
self.reload = tk.Button(text='Reload').grid(row=1, column=0, sticky='w')
self.path = tk.Entry().grid(row=1, column=1, sticky='we')
self.submit = tk.Button(text='Submit').grid(row=1, column=2, sticky='e')
root = tk.Tk()
app = ImageSelect(master=root)
app.mainloop()
采纳答案by furas
Using grid()
you can use grid_columnconfigure()
on parent of Entry
使用grid()
您可以grid_columnconfigure()
在父母上使用Entry
import tkinter as tk
root = tk.Tk()
tk.Entry(root).grid(sticky='we')
root.grid_columnconfigure(0, weight=1)
root.mainloop()
Using pack()
you could use fill='x'
使用pack()
你可以使用fill='x'
import tkinter as tk
root = tk.Tk()
tk.Entry(root).pack(fill='x')
root.mainloop()
BTW: using:
顺便说一句:使用:
self.path = tk.Entry().grid()
you assign result of grid()
to self.path
but grid()
always return None
.
您将结果分配grid()
到self.path
但grid()
始终返回None
。
If you need self.path
then do:
如果您需要,self.path
请执行以下操作:
self.path = tk.Entry()
self.path.grid()
If you don't need self.path
then you could do:
如果你不需要,self.path
那么你可以这样做:
tk.Entry().path.grid()
回答by Brionius
An Entry
widget's width is defined by the width
property. It is measured in # of characters. As far as I know there is no native way to make the Entry
automatically resize to fit a space. You can set the width like this (the default is 20):
一个Entry
窗口小部件的宽度是由定义width
属性。它以字符数来衡量。据我所知,没有本地方法可以Entry
自动调整大小以适应空间。您可以像这样设置宽度(默认为 20):
self.path = tk.Entry(width=28).grid(row=1, column=1, sticky='we')
If you really want the Entry
to automatically grow or shrink, you could bind an event to the window resizing that recalculates and and resets the necessary width of the Entry
, but it'll be kinda ugly.
如果您真的希望Entry
自动增长或缩小,您可以将事件绑定到重新计算和重置 的必要宽度的窗口调整大小Entry
,但这会有点难看。