为什么 tkinter.ttk Combobox 仅在初始选择时清除显示的值?
Why does tkinter.ttk Combobox clear displayed value on initial selection only?
我正在 tkinter 中构建一个图形用户界面,我看到了 ttk.Combobox
的奇怪行为。一切初始化都很好,但是当我从下拉列表中 select 一个项目时,组合框显示清除。这只会在我第一次制作 selection 时发生。所以我启动应用程序 > 制作一个 selection > 显示清除 > 制作另一个 selection > 显示正常。这是代码(为简洁起见,只有相关部分)。组合框 class 从数据库中获取列作为列表并将其分配给值。否则,它是非常简单的 tkinter。
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
from tkinter import N, S, E, W
from tkinter import END, CENTER
import dbmanager
class MainGUI(tk.Tk):
"""Main GUI class for EngCalc."""
def __init__(self, db_mngr):
"""See class docstring."""
self.db = db_mngr
tk.Tk.__init__(self)
tk.Tk.wm_title(self, 'EngCalc')
self.init_root_frame(master=self)
def init_root_frame(self, master=None):
"""Create and initialize main GUI container."""
self.root_frame = ttk.Frame(master)
self.root_frame.grid()
DBCombo(master=self.root_frame, controller=self,
table='[Materials(SI)]', col='Material')
class DBCombo(ttk.Combobox):
"""A dropdown combobox for a given column in database table."""
def __init__(self, master=None, controller=None,
table=None, col=None, row=0, column=0):
"""See class docstring."""
self.values = controller.db.fetch_list(table, col)
self.combovar = tk.StringVar()
ttk.Combobox.__init__(self, master, values=self.values,
textvariable=self.combovar)
self.current(0)
self.bind("<<ComboboxSelected>>", self.newselection)
self.grid(column=column, row=row)
self.state(['!disabled', 'readonly'])
def newselection(self, event):
"""Get value of combobox."""
self.combovar = self.get()
print(self.combovar)
if __name__ == '__main__':
db = '../db/test1.sqlite'
database = dbmanager.DatabaseManager(db)
foo = MainGUI(database)
foo.mainloop()
在您的代码中有一处 self.combovar
指向 StringVar
的一个实例,但稍后您将 self.combovar
重新定义为一个字符串。解决方法是 not 重新定义 self.combovar
inside newselection
我正在 tkinter 中构建一个图形用户界面,我看到了 ttk.Combobox
的奇怪行为。一切初始化都很好,但是当我从下拉列表中 select 一个项目时,组合框显示清除。这只会在我第一次制作 selection 时发生。所以我启动应用程序 > 制作一个 selection > 显示清除 > 制作另一个 selection > 显示正常。这是代码(为简洁起见,只有相关部分)。组合框 class 从数据库中获取列作为列表并将其分配给值。否则,它是非常简单的 tkinter。
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
from tkinter import N, S, E, W
from tkinter import END, CENTER
import dbmanager
class MainGUI(tk.Tk):
"""Main GUI class for EngCalc."""
def __init__(self, db_mngr):
"""See class docstring."""
self.db = db_mngr
tk.Tk.__init__(self)
tk.Tk.wm_title(self, 'EngCalc')
self.init_root_frame(master=self)
def init_root_frame(self, master=None):
"""Create and initialize main GUI container."""
self.root_frame = ttk.Frame(master)
self.root_frame.grid()
DBCombo(master=self.root_frame, controller=self,
table='[Materials(SI)]', col='Material')
class DBCombo(ttk.Combobox):
"""A dropdown combobox for a given column in database table."""
def __init__(self, master=None, controller=None,
table=None, col=None, row=0, column=0):
"""See class docstring."""
self.values = controller.db.fetch_list(table, col)
self.combovar = tk.StringVar()
ttk.Combobox.__init__(self, master, values=self.values,
textvariable=self.combovar)
self.current(0)
self.bind("<<ComboboxSelected>>", self.newselection)
self.grid(column=column, row=row)
self.state(['!disabled', 'readonly'])
def newselection(self, event):
"""Get value of combobox."""
self.combovar = self.get()
print(self.combovar)
if __name__ == '__main__':
db = '../db/test1.sqlite'
database = dbmanager.DatabaseManager(db)
foo = MainGUI(database)
foo.mainloop()
在您的代码中有一处 self.combovar
指向 StringVar
的一个实例,但稍后您将 self.combovar
重新定义为一个字符串。解决方法是 not 重新定义 self.combovar
inside newselection