【发布时间】: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