说明:单击提交按钮后,条目应出现在树中。
我限制了条目字段,因此它们只能取两个值(使用跟踪方法)。
问题1:我一共有8个entry,也就是说代码有8个StringVar,8个entry,8个label,1个button,一共17个grid。请帮我减少代码。
问题 2:我需要一个列表中的条目,我可以在全球范围内使用。
from tkinter import Tk, Frame, Button, Label, Entry, ttk, StringVar, Scrollbar
import datetime
# Main Window
class WINDOW(Tk):
def __init__(self, master):
Tk.__init__(self, master)
self.master = master
frame1 = Frame1(self)
frame1.grid(row=0,column=0)
list_of_entries=[]
class Frame1(Frame):
def __init__(self, master):
Frame.__init__(self, master,height=master.winfo_screenheight(),
width=master.winfo_screenwidth())
self.master = master
one_var = StringVar()
two_var = StringVar()
three_var = StringVar()
four_var = StringVar()
five_var = StringVar()
# Restrict entry field for 2 values only (using trace)
one_var.trace("w", lambda name, index, mode, one_var=one_var: callback())
two_var.trace("w", lambda name, index, mode, two_var=one_var: callback())
three_var.trace("w", lambda name, index, mode, three_var=one_var: callback())
four_var.trace("w", lambda name, index, mode, four_var=one_var: callback())
five_var.trace("w", lambda name, index, mode, five_var=one_var: callback())
def callback(*args):
one_var.set(one_var.get()[:2])
two_var.set(two_var.get()[:2])
three_var.set(three_var.get()[:2])
four_var.set(four_var.get()[:2])
five_var.set(five_var.get()[:2])
# Request frame labels
DATA0_lbl = Label(self, text='DATA0', font=('calibre', 10, 'bold'))
DATA1_lbl = Label(self, text='DATA1', font=('calibre', 10, 'bold'))
DATA2_lbl = Label(self, text='DATA2', font=('calibre', 10, 'bold'))
DATA3_lbl = Label(self, text='DATA3', font=('calibre', 10, 'bold'))
DATA4_lbl = Label(self, text='DATA4', font=('calibre', 10, 'bold'))
# Request frame label grid
DATA0_lbl.grid(row=0, column=0)
DATA1_lbl.grid(row=0, column=1)
DATA2_lbl.grid(row=0, column=2)
DATA3_lbl.grid(row=0, column=3)
DATA4_lbl.grid(row=0, column=4)
# Request frame entry fields
DATA0_entry = Entry(self,textvariable=one_var, width=10, font=('calibre', 10, 'normal'))
DATA1_entry = Entry(self,textvariable=two_var, width=10, font=('calibre', 10, 'normal'))
DATA2_entry = Entry(self, textvariable=three_var, width=10, font=('calibre', 10, 'normal'))
DATA3_entry = Entry(self,textvariable=four_var, width=10, font=('calibre', 10, 'normal'))
DATA4_entry = Entry(self, textvariable=five_var, width=10, font=('calibre', 10, 'normal'))
# Request frame entry field grid
DATA0_entry.grid(row=1, column=0)
DATA1_entry.grid(row=1, column=1)
DATA2_entry.grid(row=1, column=2)
DATA3_entry.grid(row=1, column=3)
DATA4_entry.grid(row=1, column=4)
# Log data sheet
NewTree = ttk.Treeview(self, height=23, columns=("DATA0","DATA1", "DATA2", "DATA3","DATA4"))
NewTree.column("#0", width=180)
NewTree.column("#1", width=150)
NewTree.column("#2", width=150)
NewTree.column("#3", width=150)
NewTree.column("#4", width=150)
NewTree.column("#5", width=150)
NewTree.heading("#0",text='TIME')
NewTree.heading("#1",text='DATA0')
NewTree.heading("#2",text='DATA1')
NewTree.heading("#3",text='DATA2')
NewTree.heading("#4",text="DATA3")
NewTree.heading("#5", text="DATA4")
NewTree.grid(row=5, columnspan=4)
def insert_data():
NewTree.insert('', 'end', text=datetime.datetime.now(),
values=(DATA0_entry.get(),
DATA1_entry.get(),
DATA2_entry.get(),
DATA3_entry.get(),
DATA4_entry.get()))
one_var.set("")
two_var.set("")
three_var.set("")
four_var.set("")
five_var.set("")
submit_button = Button(self, text="SUBMIT", command=insert_data)
submit_button.grid(row=3, column=4)
root = WINDOW(None)
root.geometry(f'{root.winfo_screenwidth()}x{root.winfo_screenheight()}')
root.title("ADD DATA")
root.mainloop()