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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:30:13  来源:igfitidea点击:

Python Tkinter - Set Entry grid width 100%

pythontkinter

提问by Patrick Burns

When i create button + entry + buttonin grid, entrywas centered but not completely fill the column. How i can fill the column via Entry?

当我button + entry + button在网格中创建时,entry居中但未完全填充该列。我如何通过填写该列Entry

enter image description here

在此处输入图片说明

# 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.pathbut grid()always return None.

您将结果分配grid()self.pathgrid()始终返回None

If you need self.paththen do:

如果您需要,self.path请执行以下操作:

self.path = tk.Entry()
self.path.grid()

If you don't need self.paththen you could do:

如果你不需要,self.path那么你可以这样做:

tk.Entry().path.grid()

回答by Brionius

An Entrywidget's width is defined by the widthproperty. It is measured in # of characters. As far as I know there is no native way to make the Entryautomatically 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 Entryto 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,但这会有点难看。