如何在 Tkinter 网格中排列左对齐标签和输入框 [英] How to line left justify label and entry boxes in Tkinter grid

查看:92
本文介绍了如何在 Tkinter 网格中排列左对齐标签和输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Tkinter 和类仍然很陌生,但我试图在 Tkinter 网格的各自列中左对齐标签和输入框.我正在使用 Justify=LEFT,但它似乎没有影响,因为标签看起来居中并且输入框从标签结束的地方开始.

I'm still pretty new to Tkinter and Classes, but I am trying to left justify labels and entry boxes each within their own column of a Tkinter grid. I am using Justify=LEFT, but it seems to have no impact as the labels look centered and the entry boxes start where the label ends.

from Tkinter import *

class LabeledEntry(Frame):
    def __init__(self, parent, *args, **kargs):
        text = kargs.pop("text")
        Frame.__init__(self, parent)
        Label(self, text=text, justify=LEFT).grid(column=0,row=0)
        Entry(self, justify=LEFT, *args, **kargs).grid(column=1, row=0)

class User_Input:
    def __init__(self, parent):
        fields = ['Text Label 1', 'This is the text Label 2']
        GUIFrame =Frame(parent)
        GUIFrame.pack(expand=True, anchor=NW)
        parent.minsize(width=350, height=325)
        field_index = 1
        for field in fields:
            self.field = LabeledEntry(GUIFrame, text=field)
            self.field.grid(column=0, row=field_index)
            field_index += 1
        self.Button2 = Button(parent, text='exit', command= parent.quit)
        self.Button2.place(x=25, y=300)

root = Tk()

MainFrame =User_Input(root)
root.mainloop()

推荐答案

我认为您的问题在于每次创建 LabeledFrame 的新实例时,您都将 入口 &标签在同一个Frame内.

I think your problem lies in the fact that each time you create a new instance of LabeledFrame, you are placing both the Entry & Label within the same Frame.

Framegrid 设置与任何其他Frame 是分开的,因此LabeledFrame 是不可能的s 对齐列,因为它们的列宽值不同.

The grid settings for this Frame are separate from any other Frame, so it is impossible for LabeledFrames to align columns as they do not have the same values for column widths.

通常要完成您的任务,只需将 sticky = W 放在 grid 选项中,以便 Entry 小部件左对齐单元格的内容.但是,这仅适用于每个单独的 Frame,使每个单独的 LabeledFrame 的内容不对齐.

Normally to accomplish what you are after you would simply put sticky = W in the grid options for the Entry widget to left-justify the contents of the cell. However, this will only work for each individual Frame, leaving the contents of each separate LabeledFrame out of alignment.

无需更改太多代码即可解决此问题的最简单方法:

Easiest way to fix this without changing much code:

您需要在 for 循环中添加一行.如果您指定插入 self.fieldFrame 的列的大的最小宽度,您可以确保事情将按照您希望的方式对齐.我还在 LabeledEntry 类中的 grid 调用中添加了配置选项: sticky = W 用于 Label&sticky = E 用于 Entry.

You'll want to add a line to your for loop. If you specify a large minimum-width of the column that self.field's Frame is inserted into, you can be sure that things will align how you want them to. I've also added config options to the grid calls within the LabeledEntry class: sticky = W for the Label & sticky = E for the Entry.

试试这个,看看它是否能解决您的问题.如果您希望该列占用更少的空间,只需减少 minsize.

Try this out and see if it solves your problem. If you would like the column to take less space simply reduce minsize.

from Tkinter import *

class LabeledEntry(Frame):
    def __init__(self, parent, *args, **kargs):
        text = kargs.pop("text")
        Frame.__init__(self, parent)
        Label(self, text=text, justify=LEFT).grid(sticky = W, column=0,row=0)
        Entry(self, *args, **kargs).grid(sticky = E, column=1, row=0)

class User_Input:
    def __init__(self, parent):
        fields = ['Text Label 1', 'This is the text Label 2']
        GUIFrame =Frame(parent)
        GUIFrame.pack(expand=True, anchor=NW)
        parent.minsize(width=350, height=325)
        field_index = 1
        for field in fields:
            self.field = LabeledEntry(GUIFrame, text=field)
            self.field.grid(column=0, row=field_index)
            self.field.grid_columnconfigure(index = 0, minsize = 150)
            field_index += 1
        self.Button2 = Button(parent, text='exit', command= parent.quit)
        self.Button2.place(x=25, y=300)

root = Tk()

MainFrame =User_Input(root)
root.mainloop()

这篇关于如何在 Tkinter 网格中排列左对齐标签和输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆