我正在从数据库中提取数据并将其放入树视图中。但是,树视图将 NULL 值显示为“none”而不是空白。当我查看数据库时,空值是空白的(空值在背景中呈非常浅的灰色)。我使用的数据库是SQLite3。我在 VS Code 中使用 VS Code 和 Gitbash 终端。问题在于,我使用 Docxtemplate.render 函数将数据填充到单词文凭模板中,但如果学生没有集中注意力,它会在集中字段中添加“无”。我只需要它是空白的。这是生成函数。 “diploma”是我的 Docxtemplate 实例化类的变量。 PS...我知道有一种更好的方法来做到这一点,而不是使用全局变量,然后创建它们,然后将它们放入字典中。我稍后会解决这个问题。
def generate():
global studentid
global fullName
global degreeType
global major1
global concentration
global gradDate
global honors
studentid = student_id_entry.get()
fullName = full_name_entry.get()
degreeType = degree_1_entry.get()
major1 = major_1_entry.get()
concentration = concentration_entry.get()
gradDate = grad_date_entry.get()
honors=honor_1_entry.get()
diploma.render({
"fullName" : fullName,
"degreeType" : degreeType,
"major": major1,
"concentration": concentration,
"honors": honors,
"graduationDate" : gradDate
})
diploma.save(f"{studentid} {fullName} {degreeType} {major1}.docx") #composite primay key
messagebox.showinfo("Success!", "The students diploma has been created!")
这是创建树视图的代码:
# Create The Treeview
my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set,
xscrollcommand=tree_scroll_bottom.set, selectmode="extended", show="headings")
my_tree.pack()
这是填充树视图的函数:
def query_database():
# Clear the Treeview
for record in my_tree.get_children():
my_tree.delete(record)
# Create a database or connect to one that exists
conn = sqlite3.connect(db)
# Create a cursor instance
c = conn.cursor()
c.execute("SELECT rowid, * FROM graduates")
records = c.fetchall()
# Add our data to the screen
global count
count = 0
#for record in records:
# print(record)
for record in records:
if count % 2 == 0:
my_tree.insert(
parent='', index='end', iid=count, text='', values=( record[1], record[2],
record[3], record[4], record[5], record[6], record[7], record[8], record[9],
record[10], record[11], record[12]), tags=('evenrow',))
else:
my_tree.insert(parent='', index='end', iid=count, text='', values=( record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[8], record[9], record[10], record[11], record[12]), tags=('oddrow',))
# increment counter
count += 1
# Commit changes
conn.commit()
# Close our connection
conn.close()
任何帮助将不胜感激。
将评论转化为实际答案:
基本上,您可以在设置
concentration
的值时执行条件操作,只需将值 "none"
转换为空字符串 ""
concentration = concentration_entry.get() if concentration_entry.get() != "none" else ""