我正在用一个listbox构建一个pythontkinter软件,但是listbox不会从一个列表(在字典中)到for循环加载值。你知道吗
字典存储在一个带有numpy的.npy文件中,其结构如下:m={"subject":["subjectname"], ...}
。但我不认为问题是由文件或字典引起的,因为它与ttk组合框配合得很好。。。你知道吗
这是我的密码:
import os.path
import numpy as np
global m
global fn_sub
global path
m={}
# filename for the file to save
fn_sub = "subjects.npy"
path = os.path.expanduser(r'~\Documents\School Life Diary')
# check the directory does not exist
if not(os.path.exists(os.path.join(path,fn_sub))):
# write the file in the new directory
np.save(os.path.join(path, fn_sub), m)
from tkinter import *
import tkinter.messagebox
from tkinter.ttk import *
global old_mat
def inizializza():
try:
m=np.load(os.path.join(path, fn_sub)).item()
except ValueError:
m={}
### other code... ###
def creaFinestra():
global wim
wim=Toplevel()
inizializza()
wim.title("Materie - School Life Diary")
wim.iconbitmap("sld_icon_beta.ico")
wim.geometry("%dx%d+%d+%d" % (325, 325, 600, 200))
s=Style()
try:
s.theme_use("vista")
except:
s.theme_use()
fim=Frame(wim)
fim.pack()
global lb
lb=Listbox(fim,selectmode="SINGLE")
lb.pack(padx=10,pady=10)
for e in m:
lb.insert("end",*m[e])
''' I also used this, but doesn't work...
for e in m:
lb.insert(list(m.keys()).index(e),m[e][0])
'''
fim2=Frame(wim)
fim2.pack()
imageAdd=PhotoImage(file=r"images/add_FAB.png")
imageMod=PhotoImage(file=r"images/mod_FAB.png")
imageDel=PhotoImage(file=r"images/trash_FAB.png")
bAdd=Button(fim2,image=imageAdd,command=add)
bAdd.image=imageAdd
bMod=Button(fim2,image=imageMod,command=edit)
bDel=Button(fim2,image=imageDel,command=delete)
bAdd.grid(row=0,column=0,padx=10,pady=10)
bMod.grid(row=0,column=1,padx=10,pady=10)
bDel.grid(row=0,column=2,padx=10,pady=10)
wim.mainloop()
你能帮我吗? 谢谢
发现问题: Numpy加载字典,但是如果不在前面添加
global m
,变量m将永远丢失。 所以我重新检查了代码并添加了一些控件。这里是完整的功能代码:谢谢你的帮助!你知道吗
相关问题 更多 >
编程相关推荐