[英]Format String for tk.Listbox
I want to fill a tk.Listbox.我想填写一个 tk.Listbox。 On the left side there should be the names of the indicators (left-justified), on the right side the values and the units (right-justified).左侧应该是指标的名称(左对齐),右侧是值和单位(右对齐)。 There are some... in between.有一些……介于两者之间。
In Jupyter it works:在 Jupyter 中它可以工作:
enter image description here在此处输入图像描述
In a GUI it looks like this:在 GUI 中,它看起来像这样:
enter image description here在此处输入图像描述
The code is:代码是:
for name, value in zip(content_name, content_value):
points = 34 - len(name) - len(value)
in_between = "........"
for p in range(points):
in_between = in_between + "."
content = name + in_between + value
listbox_record.insert(tk.END, content)
Is there a way to do this in a GUI without knowing the font or the specific length of a character in pixels?有没有办法在不知道字体或字符的特定长度(以像素为单位)的情况下在 GUI 中执行此操作?
Thanks!谢谢!
As suggested by acw1668, I've set the font to a mono spaced font ("Courier", 9).正如 acw1668 所建议的,我已将字体设置为 mono 间隔字体(“Courier”,9)。 I believe TKinter default font is ("New Courier", 9).我相信 TKinter 默认字体是(“New Courier”,9)。
### Frame Listbox
frame_listbox = tk.Frame(self.frame_activities)
frame_listbox.grid(row = 1, column = 0, columnspan = 2, sticky = "ew", padx = 10)
### Frame Scrollbar
frame_scroll_select_activity = tk.Frame(frame_listbox)
scrollbar_select_activity = tk.Scrollbar(frame_scroll_select_activity, orient = tk.VERTICAL)
### Font Listbox
font_listbox = ("Courier", 9)
### Listbox
listbox_record = tk.Listbox(frame_scroll_select_activity, font = font_listbox, width = 42, yscrollcommand = scrollbar_select_activity.set, selectmode = tk.SINGLE)
### Configure Scrollbar
scrollbar_select_activity.config(command = listbox_record.yview)
### Packing
scrollbar_select_activity.pack(side = tk.RIGHT, fill = tk.Y)
frame_scroll_select_activity.pack()
listbox_record.pack(pady = 10)
Fill listbox...填充列表框...
for name, value in zip(content_name, content_value):
content = name + value.rjust(42 - len(name), ".")
listbox_record.insert(tk.END, content)
And it looks like that...而且看起来是这样的……
Very cool!很酷!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.