【问题标题】:align text in text widget vertically垂直对齐文本小部件中的文本
【发布时间】:2014-12-10 17:43:47
【问题描述】:

我正在使用 tkinter 的网格几何管理器来创建一个包含文本、条目和标签小部件的“表格”。 文本小部件(蓝色)用于显示要由用户输入到相应条目小部件中的变量名称(例如“theta_air”),而标签小部件显示适当的单位(例如“°​​C”)。

我已经将条目和标签小部件垂直对齐。如文本小部件的蓝色背景所示,文本小部件本身已正确对齐。但是,我无法垂直对齐文本小部件的文本。或者,换句话说,文本小部件的基线未与条目或标签小部件的基线对齐。

如何垂直对齐文本小部件的文本或实现类似的视觉行为? 下面显示了屏幕截图和相应的代码 sn-p(不能独立工作)。

self.parameternames = [
    {'name': 'a', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'b', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'c', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'd', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'e', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'f', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'g', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'h', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'i', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'j', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'k', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'l', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'm', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'n', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.},
    {'name': 'o', 'unit': '\u00b0C', 'coords': (0, 0), 'default': 0.}]

for i, subdict in enumerate(self.parameternames):
    r = i % 4
    c = (i // 4) * 3
    self.lowerframe.grid_columnconfigure(c, weight=1)
    self.lowerframe.grid_columnconfigure(c+2, weight=1)
    t = Text(self.lowerframe, width=5, height=2, background='#336699', font=('', 12))
    t.tag_configure('subscript', offset=-4, font=('', 10))
    t.tag_configure('justify_right', justify='right')
    t.insert('insert', '\u03d1', 'justify_right', 'air', 'subscript')
    t.configure(state='disabled')
    t.grid(row=r, column=c, pady=5, padx=0, sticky='NSE')
    Label(self.lowerframe, text=subdict['unit'], anchor=W).grid(row=r, column=c+2, pady=5, padx=0, sticky='W')
    subdict['var_table'] = StringVar()
    entry_table = Entry(self.lowerframe, width=10, justify='right', textvariable=subdict['var_table'])
    entry_table.grid(row=r, column=c+1, pady=5, padx=0, sticky='EW')
    subdict['pname_table'] = str(entry_table)

【问题讨论】:

    标签: python python-3.x text tkinter grid


    【解决方案1】:

    首先我使用了height=1,它似乎剪切了下标文本,所以我尝试使用height=2。为了证明索引/订阅是否被切断,我使用了另一个索引,例如 dryer,其中包含低于索引基线的“y”。

    但是,如果使用height=1,则下标根本不会被截断,如下面的截图所示:

    修改后的代码行如下:

    t = Text(self.lowerframe, width=5, height=1, background='#336699', font=('', 12))
    

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多