【问题标题】:Saving and re-opening data in text boxes in Tkinter在 Tkinter 的文本框中保存和重新打开数据
【发布时间】:2020-09-13 03:40:12
【问题描述】:

我在学校的最后一年,即 12 年级,我正在尝试制作一个我的客户能够打开的每周计划程序,将他们的数据输入到分配的文本框中,然后点击“保存”按钮并关闭窗口,当他们重新打开窗口时,他们之前输入的数据将已经存在。我已经创建了我的窗口、标题、文本框网格和一个“清除”按钮,用于清除文本框中的所有数据。我创建了“保存”按钮,但没有链接到它的命令。 这是我的代码:(它很长,其中大部分可能是不必要的,但它可以满足我的需求):

from tkinter import Scale,Tk,Frame,Label,Button
from tkinter.ttk import Notebook,Entry,Button

root = Tk()
#title of the window
root.title('Orgnaiser')
#how big the window is in pixels
root.geometry("1700x700")


#program sets up a range of rows and columns:
for row in range(21):
    for column in range(7):#program then checks for the required info of row and column number
        if row==0 and column==0:#if these numbers match:
            label = Label(root, text="Monday")#the heading text of the first column will be "Monday",
            label.config(font=('comic sans ms', 16))#the font of the heading will be "comic sans ms"
            label.grid(row=row, column=column, sticky="nsew")
            root.grid_columnconfigure(column, weight=1)
        if row==0 and column==1:#repeats the same process as before but changes the heading text to "Tuesday",
            label = Label(root, text="Tuesday")# and moves it a column over to the right
            label.config(font=('comic sans ms', 16))
            label.grid(row=row, column=column, sticky="nsew")
            root.grid_columnconfigure(column, weight=1)
        if row==0 and column==2:#repeats the same process as before but changes the heading text to "Wednesday",
            label = Label(root, text="Wednesday")# and moves it a column over to the right
            label.config(font=('comic sans ms', 16))
            label.grid(row=row, column=column, sticky="nsew")
            root.grid_columnconfigure(column, weight=1)
        if row==0 and column==3:#repeats the same process as before but changes the heading text to "Thursday",
            label = Label(root, text="Thursday")# and moves it a column over to the right
            label.config(font=('comic sans ms', 16))
            label.grid(row=row, column=column, sticky="nsew")
            root.grid_columnconfigure(column, weight=1)
        if row==0 and column==4:#repeats the same process as before but changes the heading text to "Friday",
            label = Label(root, text="Friday")# and moves it a column over to the right
            label.config(font=('comic sans ms', 16))
            label.grid(row=row, column=column, sticky="nsew")
            root.grid_columnconfigure(column, weight=1)
        if row==0 and column==5:#repeats the same process as before but changes the heading text to "Saturday",
            label = Label(root, text="Saturday")# and moves it a column over to the right
            label.config(font=('comic sans ms', 16))
            label.grid(row=row, column=column, sticky="nsew")
            root.grid_columnconfigure(column, weight=1)
        if row==0 and column==6:#repeats the same process as before but changes the heading text to "Sunday",
            label = Label(root, text="Sunday")# and moves it a column over to the right
            label.config(font=('comic sans ms', 16))
            label.grid(row=row, column=column, sticky="nsew")
            root.grid_columnconfigure(column, weight=1)
            
#The next process is to name the textboxes and sort out their positions
##This is very repetative task with the only diference between each of them is their name and position,
##grouped together within each day of the week.
        if row==1 and column==0:#text box is placed in specified spot in the grid
            a = Entry(root, text="")
            a.grid(row=1, column=0, padx=5, pady=5)

        if row==2 and column==0:##this same process is repeated for each specified grid space
            aa = Entry(root, text="")
            aa.grid(row=2, column=0, padx=5, pady=5)
            
        if row==3 and column==0:
            ab = Entry(root, text="")##tell the program that the text box can be edited/typed in
            ab.grid(row=3, column=0, padx=5, pady=5)
            
        if row==4 and column==0:
            ac = Entry(root, text="")
            ac.grid(row=4, column=0, padx=5, pady=5)##confirms the placement and arrangement
                                                    ###of the textbox in the grid

        if row==5 and column==0:
            ad = Entry(root, text="")
            ad.grid(row=5, column=0, padx=5, pady=5)
            
        if row==6 and column==0:
            ae = Entry(root, text="")
            ae.grid(row=6, column=0, padx=5, pady=5)
            
        if row==7 and column==0:
            af = Entry(root, text="")
            af.grid(row=7, column=0, padx=5, pady=5)

        if row==8 and column==0:
            ag = Entry(root, text="")
            ag.grid(row=8, column=0, padx=5, pady=5)
            
        if row==9 and column==0:
            ah = Entry(root, text="")
            ah.grid(row=9, column=0, padx=5, pady=5)
            
        if row==10 and column==0:
            ai = Entry(root, text="")
            ai.grid(row=10, column=0, padx=5, pady=5)

        
        


#Tuesday textboxes
##Process repeats as before

        if row==1 and column==1:
            b = Entry(root, text="")
            b.grid(row=1, column=1, padx=5, pady=5)

        if row==2 and column==1:
            ba = Entry(root, text="")
            ba.grid(row=2, column=1, padx=5, pady=5)
            
        if row==3 and column==1:
            bb = Entry(root, text="")
            bb.grid(row=3, column=1, padx=5, pady=5)
            
        if row==4 and column==1:
            bc = Entry(root, text="")
            bc.grid(row=4, column=1, padx=5, pady=5)

        if row==5 and column==1:
            bd = Entry(root, text="")
            bd.grid(row=5, column=1, padx=5, pady=5)
            
        if row==6 and column==1:
            be = Entry(root, text="")
            be.grid(row=6, column=1, padx=5, pady=5)
            
        if row==7 and column==1:
            bf = Entry(root, text="")
            bf.grid(row=7, column=1, padx=5, pady=5)

        if row==8 and column==1:
            bg = Entry(root, text="")
            bg.grid(row=8, column=1, padx=5, pady=5)
            
        if row==9 and column==1:
            bh = Entry(root, text="")
            bh.grid(row=9, column=1, padx=5, pady=5)
            
        if row==10 and column==1:
            bi = Entry(root, text="")
            bi.grid(row=10, column=1, padx=5, pady=5)

        
        
            
        
        
#Wednesday textboxes
#Process repeats as before
        
        if row==1 and column==2:
            c = Entry(root, text="")
            c.grid(row=1, column=2, padx=5, pady=5)

        if row==2 and column==2:
            ca = Entry(root, text="")
            ca.grid(row=2, column=2, padx=5, pady=5)
            
        if row==3 and column==2:
            cb = Entry(root, text="")
            cb.grid(row=3, column=2, padx=5, pady=5)
            
        if row==4 and column==2:
            cc = Entry(root, text="")
            cc.grid(row=4, column=2, padx=5, pady=5)

        if row==5 and column==2:
            cd = Entry(root, text="")
            cd.grid(row=5, column=2, padx=5, pady=5)
            
        if row==6 and column==2:
            ce = Entry(root, text="")
            ce.grid(row=6, column=2, padx=5, pady=5)
            
        if row==7 and column==2:
            cf = Entry(root, text="")
            cf.grid(row=7, column=2, padx=5, pady=5)

        if row==8 and column==2:
            cg = Entry(root, text="")
            cg.grid(row=8, column=2, padx=5, pady=5)
            
        if row==9 and column==2:
            ch = Entry(root, text="")
            ch.grid(row=9, column=2, padx=5, pady=5)
            
        if row==10 and column==2:
            ci = Entry(root, text="")
            ci.grid(row=10, column=2, padx=5, pady=5)

        
        
        
        
        
#Thursday textboxes
#Process repeats as before
        
        if row==1 and column==3:
            d = Entry(root, text="")
            d.grid(row=1, column=3, padx=5, pady=5)

        if row==2 and column==3:
            da = Entry(root, text="")
            da.grid(row=2, column=3, padx=5, pady=5)
            
        if row==3 and column==3:
            db = Entry(root, text="")
            db.grid(row=3, column=3, padx=5, pady=5)
            
        if row==4 and column==3:
            dc = Entry(root, text="")
            dc.grid(row=4, column=3, padx=5, pady=5)

        if row==5 and column==3:
            dd = Entry(root, text="")
            dd.grid(row=5, column=3, padx=5, pady=5)
            
        if row==6 and column==3:
            de = Entry(root, text="")
            de.grid(row=6, column=3, padx=5, pady=5)
            
        if row==7 and column==3:
            df = Entry(root, text="")
            df.grid(row=7, column=3, padx=5, pady=5)

        if row==8 and column==3:
            dg = Entry(root, text="")
            dg.grid(row=8, column=3, padx=5, pady=5)
            
        if row==9 and column==3:
            dh = Entry(root, text="")
            dh.grid(row=9, column=3, padx=5, pady=5)
            
        if row==10 and column==3:
            di = Entry(root, text="")
            di.grid(row=10, column=3, padx=5, pady=5)

        
        
        
            
            
#Friday textboxes
#Process repeats as before
        
        if row==1 and column==4:
            e = Entry(root, text="")
            e.grid(row=1, column=4, padx=5, pady=5)

        if row==2 and column==4:
            ea = Entry(root, text="")
            ea.grid(row=2, column=4, padx=5, pady=5)
            
        if row==3 and column==4:
            eb = Entry(root, text="")
            eb.grid(row=3, column=4, padx=5, pady=5)
            
        if row==4 and column==4:
            ec = Entry(root, text="")
            ec.grid(row=4, column=4, padx=5, pady=5)

        if row==5 and column==4:
            ed = Entry(root, text="")
            ed.grid(row=5, column=4, padx=5, pady=5)
            
        if row==6 and column==4:
            ee = Entry(root, text="")
            ee.grid(row=6, column=4, padx=5, pady=5)
            
        if row==7 and column==4:
            ef = Entry(root, text="")
            ef.grid(row=7, column=4, padx=5, pady=5)

        if row==8 and column==4:
            eg = Entry(root, text="")
            eg.grid(row=8, column=4, padx=5, pady=5)
            
        if row==9 and column==4:
            eh = Entry(root, text="")
            eh.grid(row=9, column=4, padx=5, pady=5)
            
        if row==10 and column==4:
            ei = Entry(root, text="")
            ei.grid(row=10, column=4, padx=5, pady=5)

        
        
        
            
            
#Saturday textboxes
#Process repeats as before
        
        if row==1 and column==5:
            f = Entry(root, text="")
            f.grid(row=1, column=5, padx=5, pady=5)

        if row==2 and column==5:
            fa = Entry(root, text="")
            fa.grid(row=2, column=5, padx=5, pady=5)
            
        if row==3 and column==5:
            fb = Entry(root, text="")
            fb.grid(row=3, column=5, padx=5, pady=5)
            
        if row==4 and column==5:
            fc = Entry(root, text="")
            fc.grid(row=4, column=5, padx=5, pady=5)

        if row==5 and column==5:
            fd = Entry(root, text="")
            fd.grid(row=5, column=5, padx=5, pady=5)
            
        if row==6 and column==5:
            fe = Entry(root, text="")
            fe.grid(row=6, column=5, padx=5, pady=5)
            
        if row==7 and column==5:
            ff = Entry(root, text="")
            ff.grid(row=7, column=5, padx=5, pady=5)

        if row==8 and column==5:
            fg = Entry(root, text="")
            fg.grid(row=8, column=5, padx=5, pady=5)
            
        if row==9 and column==5:
            fh = Entry(root, text="")
            fh.grid(row=9, column=5, padx=5, pady=5)
            
        if row==10 and column==5:
            fi = Entry(root, text="")
            fi.grid(row=10, column=5, padx=5, pady=5)

        
        
        
        
#Sunday textboxes
#Process repeats as before
        
        if row==1 and column==6:
            g = Entry(root, text="")
            g.grid(row=1, column=6, padx=5, pady=5)

        if row==2 and column==6:
            ga = Entry(root, text="")
            ga.grid(row=2, column=6, padx=5, pady=5)
            
        if row==3 and column==6:
            gb = Entry(root, text="")
            gb.grid(row=3, column=6, padx=5, pady=5)
            
        if row==4 and column==6:
            gc = Entry(root, text="")
            gc.grid(row=4, column=6, padx=5, pady=5)

        if row==5 and column==6:
            gd = Entry(root, text="")
            gd.grid(row=5, column=6, padx=5, pady=5)
            
        if row==6 and column==6:
            ge = Entry(root, text="")
            ge.grid(row=6, column=6, padx=5, pady=5)
            
        if row==7 and column==6:
            gf = Entry(root, text="")
            gf.grid(row=7, column=6, padx=5, pady=5)

        if row==8 and column==6:
            gg = Entry(root, text="")
            gg.grid(row=8, column=6, padx=5, pady=5)
            
        if row==9 and column==6:
            gh = Entry(root, text="")
            gh.grid(row=9, column=6, padx=5, pady=5)
            
        if row==10 and column==6:
            gi = Entry(root, text="")
            gi.grid(row=10, column=6, padx=5, pady=5)

        
            

#This is the "Clear" button, to clear all the inputs that the user enters into all the textboxes

def clearTextInput():
    a.delete("0", "end")
    aa.delete("0", "end")
    ab.delete("0", "end")
    ac.delete("0", "end")
    ad.delete("0", "end")
    ae.delete("0", "end")
    af.delete("0", "end")
    ag.delete("0", "end")
    ah.delete("0", "end")
    ai.delete("0", "end")

    
    b.delete("0", "end")
    ba.delete("0", "end")
    bb.delete("0", "end")
    bc.delete("0", "end")
    bd.delete("0", "end")
    be.delete("0", "end")
    bf.delete("0", "end")
    bg.delete("0", "end")
    bh.delete("0", "end")
    bi.delete("0", "end")

    
    c.delete("0", "end")
    ca.delete("0", "end")
    cb.delete("0", "end")
    cc.delete("0", "end")
    cd.delete("0", "end")
    ce.delete("0", "end")
    cf.delete("0", "end")
    cg.delete("0", "end")
    ch.delete("0", "end")
    ci.delete("0", "end")

    
    d.delete("0", "end")
    da.delete("0", "end")
    db.delete("0", "end")
    dc.delete("0", "end")
    dd.delete("0", "end")
    de.delete("0", "end")
    df.delete("0", "end")
    dg.delete("0", "end")
    dh.delete("0", "end")
    di.delete("0", "end")

    
    e.delete("0", "end")
    ea.delete("0", "end")
    eb.delete("0", "end")
    ec.delete("0", "end")
    ed.delete("0", "end")
    ee.delete("0", "end")
    ef.delete("0", "end")
    eg.delete("0", "end")
    eh.delete("0", "end")
    ei.delete("0", "end")

    
    f.delete("0", "end")
    fa.delete("0", "end")
    fb.delete("0", "end")
    fc.delete("0", "end")
    fd.delete("0", "end")
    fe.delete("0", "end")
    ff.delete("0", "end")
    fg.delete("0", "end")
    fh.delete("0", "end")
    fi.delete("0", "end")

    
    g.delete("0", "end")
    ga.delete("0", "end")
    gb.delete("0", "end")
    gc.delete("0", "end")
    gd.delete("0", "end")
    ge.delete("0", "end")
    gf.delete("0", "end")
    gg.delete("0", "end")
    gh.delete("0", "end")
    gi.delete("0", "end")


#------------------------------------------------------------------------------------------------#
##'Save' button is yet to be programmed but will keep the inputed data saved to the textboxes,
## even if the window is closed and re-opened 
save_button = Button(root, text ="Save")
save_button.grid(row=19, column=2, padx=10, pady=10)
#------------------------------------------------------------------------------------------------#

#A 'Clear' button, once activated, will clear all the text boxes in the window
clear_button = Button(root, text ="Clear", command=clearTextInput)
clear_button.grid(row=19, column=4, padx=10, pady=10)#Where within the grid the button is placed


root.mainloop()

我对编码有点陌生,这是我唯一需要完成我的代码的事情。我将非常感谢任何可以帮助我解决这个问题的人,因为我已经坚持了一段时间。 谢谢。

【问题讨论】:

  • 您必须将数据保存到外部文件。根据您的需要,您可以考虑纯文本文件(不推荐用于机密数据)、Shelve(允许以漂亮的字典格式将数据保存在数据结构中(非常适合常见需求))、sqlite3(MySQL 的轻量级版本)中的任何内容并且非常适合独立的桌面应用程序)。现在,有关如何保存数据的更多帮助,请尝试将您的代码缩减为 Minimal Repoducible Example

标签: python tkinter


【解决方案1】:

对于您的规划器脚本,有两件事:

  • 为网格创建条目对象列表。这比单独的变量更简洁(也更小)。
  • 使用条目列表,遍历条目以将值保存\读取到 csv 文件

试试这个代码:

from tkinter import Scale,Tk,Frame,Label,Button
from tkinter.ttk import Notebook,Entry,Button

root = Tk()
#title of the window
root.title('Organiser')
#how big the window is in pixels
root.geometry("1700x700")

# create label for each day
daylist = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
for i,dy in enumerate(daylist):
    label = Label(root, text=dy)#the heading text of the first column will be "Monday",
    label.config(font=('comic sans ms', 16))#the font of the heading will be "comic sans ms"
    label.grid(row=0, column=i, sticky="nsew")
    root.grid_columnconfigure(i, weight=1)

entrylist = []  # list of all entry objects
for c in range(7):  # each column
   entrylist.append([])  # inner list for each row
   for r in range(10):  # each row
       a = Entry(root, text="")  # create entry object
       a.grid(row=r+1, column=c, padx=5, pady=5)
       entrylist[c].append(a)  # add to list

def clearTextInput():  # clear all entry boxes
    for c in entrylist:
        for r in c:
           r.delete("0", "end")

def save():
    import csv
    with open('plan.csv', 'w', newline='') as f:
        wr = csv.writer(f, quoting=csv.QUOTE_ALL)
        for r in range(10):
           lst = []  # create list for each row of data
           for c in range(7):  # each column
              lst.append(entrylist[c][r].get())
           wr.writerow(lst)  # write row to csv
    
def load():
    clearTextInput()  # prevent mixed text
    import csv
    with open('plan.csv') as f:  # read csv file
        rd = csv.reader(f)
        for r,data in enumerate(rd): # read single row (list)
           for c in range(7):  # each column
              entrylist[c][r].insert(0, data[c])  # fill in text box
   

load_button = Button(root, text ="Load", command=load)
load_button.grid(row=19, column=2, padx=10, pady=10)
   
save_button = Button(root, text ="Save", command=save)
save_button.grid(row=19, column=3, padx=10, pady=10)

clear_button = Button(root, text ="Clear", command=clearTextInput)
clear_button.grid(row=19, column=4, padx=10, pady=10)

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2012-12-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多