[英]Python 3.3: tkinter.ttk combobox error when pulling the currently selected value
[英]Why does tkinter.ttk Combobox clear displayed value on initial selection only?
我正在ttk.Combobox
構建gui,並且看到了ttk.Combobox
的奇怪行為。 一切初始化都很好,但是當我從下拉列表中選擇一個項目時,組合框顯示將清除。 這僅在我第一次選擇時發生。 因此,我啟動了應用程序>進行選擇> 顯示清除 >進行其他選擇>正常播放。 這是代碼(為簡潔起見,僅是相關部分)。 combobox類從數據庫中獲取一列作為列表,並將其分配給值。 否則,它是相當簡單的。
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
重新定義為字符串。 解決方案是不在 self.combovar
內部重新定義newselection
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.