我正在编写一个小程序,它从SQLite DB中获取输入,并根据Combobox中的选择在GUI中打印出信息。基本上它是游戏自由职业者的交易信息(这些商品在特定的基础上出售)。我得到它的工作,它打印出树视图中的第一个选择。但是,当我想要打印下一个(不同的)选择时,它不会刷新,但会在第一个之后创建一个新的Treeview。我尝试了几个东西,包括一个destroy(),但我无法让它工作。以下是我写的代码。我编辑了一些固定数据而不是从SQLite DB中提取数据。 “Bases”按钮填充组合框。 “基本销售的商品”按钮采用组合框的选择并生成树视图。
from Tkinter import *
import csv, sqlite3, sys
from ttk import Combobox, Treeview
root = Tk()
root.minsize(width=500, height=300)
v = StringVar() # ========= v is a variable that stores the selection(s) from the combobox ============
dropdownBases = []
goodsSoldAtBase = []
tree_header = []
def on_field_change(index, value, op):
v = boxBases.get()
print 'v is', v
def List_bases():
data = ['base1', 'base2', 'base3', 'base4',]
for i in data:
dropdownBases.append(i)
boxBases.config(values=dropdownBases)
def List_sold_at_base():
destroy_widget()
sb=v.get()
print sb
dataFromDB = [['base1', 'Cobalt 275', 'Copper 180'],['base2', 'High Temp Alloy 280', 'Mining Machinery 130'],
['base3', 'H-Fuel 240', 'Oxygen 6', 'Water 18'], ['base4', 'Hydrocarbons 40', 'Polymers 107']]
for i in dataFromDB:
if i[0] == sb:
goodsSoldAtBase.append(i[1])
goodsSoldAtBase.append(i[2])
# --------------- print for control purpose-------------------
print i
print "\n", dataFromDB
print "\n", goodsSoldAtBase
tree_header = ["Commodity Name", "Price"]
#================== build the treeview and populate it =============================
container = Frame(printLabel)
container.pack(side= TOP, fill='both', expand=True)
# create a treeview with scrollbar
tree = Treeview(columns=tree_header, show="headings")
vsb = Scrollbar(orient="vertical", command=tree.yview)
tree.grid(column=0, row=0, sticky='nsew', in_=container)
vsb.grid(column=1, row=0, sticky='ns', in_=container)
container.grid_columnconfigure(0, weight=1)
container.grid_rowconfigure(0, weight=1)
for col in tree_header:
tree.heading(col, text=col.title())
for item in goodsSoldAtBase:
tree.insert('', 'end', values=item)
# adjust column's width if necessary to fit each value
def destroy_widget():
try:
container.destroy()
except:
pass
def selection(event):
v=boxBases.current()
def file_menu():
root.title("Freelancer trade helper")
menubar = Menu(root)
root.config(menu=menubar)
fileMenu = Menu(menubar)
fileMenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="File", menu=fileMenu)
# ================== Frames ===========================
leftFrame= Frame(root)
leftFrame.pack(side=LEFT, fill=BOTH)
rightFrame= Frame(root)
rightFrame.pack(side=RIGHT, fill=BOTH, expand=TRUE)
printLabel = Label(rightFrame, relief=SUNKEN)
printLabel .pack(side=BOTTOM, fill=BOTH, expand=TRUE)
# ================== Buttons ==========================
boxBases = Combobox(rightFrame, textvar=v, values=dropdownBases)
boxBases.bind('<<ComboboxSelected>>', selection)
boxBases.pack(side=TOP, anchor=NW, fill=X)
baseButton=Button(leftFrame, text="Bases", command=List_bases)
baseButton.pack(side=TOP, anchor=W, fill=X)
goodsButton = Button(leftFrame, text="Goods sold at base", command=List_sold_at_base)
goodsButton.pack(side=TOP)
file_menu()
root.mainloop()
答案 0 :(得分:0)
为什么不清除旧数据并将新数据放在同一个小部件中,而不是销毁和重新制作整个小部件?