Python Tkinter - 设置条目网格宽度 100% [英] Python Tkinter - Set Entry grid width 100%
本文介绍了Python Tkinter - 设置条目网格宽度 100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在网格中创建 button + entry + button
时,entry
居中但未完全填充该列.我如何通过 Entry
填充该列?
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
?
# 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()
推荐答案
使用 grid()
你可以在 Entry
的父级上使用 grid_columnconfigure()
代码>
Using grid()
you can use grid_columnconfigure()
on parent of Entry
import tkinter as tk
root = tk.Tk()
tk.Entry(root).grid(sticky='we')
root.grid_columnconfigure(0, weight=1)
root.mainloop()
<小时>
使用 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()
您将 grid()
的结果分配给 self.path
但 grid()
总是返回 None
.
you assign result of grid()
to self.path
but grid()
always return None
.
如果你需要 self.path
然后做:
If you need self.path
then do:
self.path = tk.Entry()
self.path.grid()
如果你不需要 self.path
那么你可以这样做:
If you don't need self.path
then you could do:
tk.Entry().path.grid()
这篇关于Python Tkinter - 设置条目网格宽度 100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文