简体   繁体   English

tk.Listbox 的格式字符串

[英]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...而且看起来是这样的……

Listbox列表框

Very cool!很酷!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不使用 tk.Button 的情况下从 tk.Text 和 tk.Listbox 获取用户输入 - Get user input from tk.Text and tk.Listbox without using tk.Button 子类化 tk.Listbox 时出错(属性错误对象没有属性 &#39;tk&#39;) - Error when subclassing tk.Listbox (Attribute error object has no attribute 'tk') 如何在ttk.Combobox中更改下拉菜单(即tk.Listbox)的活动背景的颜色 - How to change the color of the active background of the dropdown menu (i.e. tk.Listbox) in a ttk.Combobox 在Python Tk应用中调整列表框的大小 - Resizing a Listbox in Python Tk app 如何将mousewheel绑定到tk inter _listbox? - How bind mousewheel to tk inter _listbox? Python tk 列表框上的滚动条在 windows 10 中不起作用 - Python tk scrollbar on listbox not working in windows 10 python tkinter 列表框与其他 tk/ttk 小部件的性能 - python tkinter listbox performance with other tk/ttk widgets 将“tk.ACTIVE”转换为列表框中的索引(行号) - “Convert” tk.ACTIVE to the index (line number) in a listbox 是否可以更改 Tk/Tkinter 列表框小部件的绑定顺序 &lt;<listboxselect> &gt; 活动</listboxselect> - Is it possible to alter binding order of a Tk/Tkinter Listbox widget <<ListboxSelect>> event 如何将文件夹中的文件列出到 TK 列表框 - Python3 - How to list files in a folder to a TK Listbox - Python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM