简体   繁体   English

python tkinter 列表框与其他 tk/ttk 小部件的性能

[英]python tkinter listbox performance with other tk/ttk widgets

I tried to isolate the problem as best as i could.我试图尽可能地隔离问题。

Lets assume that there are 3 tk/ttk widgets.让我们假设有 3 个 tk/ttk 小部件。 Why doesn't the tk_spinbox unselect the selected listbox entry and why does the ttk_spinbox?为什么 tk_spinbox 不取消选择选定的列表框条目,为什么 ttk_spinbox? I dont want to unselect the items whenever the ttk_spinbox is pressed.每当按下 ttk_spinbox 时,我都不想取消选择这些项目。 Is there an workaround in order to get an equal behaviour for ttk_spinbox and tk_spinbox?是否有一种解决方法可以使 ttk_spinbox 和 tk_spinbox 具有相同的行为?

Here is the code:这是代码:

import tkinter as tk
import tkinter.ttk as ttk

masterframe = tk.Tk()

listbox = tk.Listbox(masterframe, height=5, selectmode='multiple')
listbox.pack(padx=10, pady=10)
listbox.insert(tk.END, 'blubb_1')
listbox.insert(tk.END, 'blubb_2')

tk_spinbox = tk.Spinbox(masterframe,from_=10, to=20, increment=2)
tk_spinbox.pack(padx=10, pady=10)

ttk_spinbox = ttk.Spinbox(masterframe,from_=10, to=20, increment=2)
ttk_spinbox.pack(padx=10, pady=10)

masterframe.mainloop()

The "why" is simply, that's how they are designed to work. “为什么”很简单,这就是它们的设计方式。 When you interact with a ttk spinbox, the spinbox value is automatically selected.当您与 ttk spinbox 交互时,将自动选择 spinbox 值。 This doesn't happen for the tk spinbox. tk spinbox 不会发生这种情况。 By default only one thing can have the selection at a time, so the listbox loses the selection when the spinbox gains the selection.默认情况下,一次只能选择一件事,因此当旋转框获得选择时,列表框会丢失选择。

If you don't want to have the listbox lose the selection you can set the exportselection option to False on the listbox and/or the ttk spinbox.如果您不想让列表框失去选择,您可以在列表框和/或 ttk 旋转框exportselection选项设置为 False。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM