Tkinter-使用所有小部件打开并保存UI。递增行时出现问题

时间:2016-02-23 22:04:45

标签: python tkinter

这是我写的代码片段。我必须使Open和Save Button功能正常。所以,我的两个功能都很好。我能够保存并加载UI,但基本问题是加载后,当我点击 添加行 时,行已经添加到已经下方存在的行。已经有一周的时间了。我遇到麻烦,不知道出路

from tkinter import *
import dill
from collections import OrderedDict
class Program: 
    def __init__(self):
        self.row=0
        self.entries=[]
        self.current_widget=0
        self.Ordered_dictionary_for_entry_widget=OrderedDict() 
        self.values_for_entry_dictionary=[]
        self.AddButton = Button(text="Add Row", command=self.add_button_command)
        self.AddButton.grid(column=0,row=0)
        self.save_button=Button(text="save",command=self.save_button_command)
        self.save_button.grid(column=0,row=1)
        self.load_button=Button(text="Open",command=self.Open_button_command)
        self.load_button.grid(column=0,row=2)
        self.total_entries_length=len(self.entries)
    def add_button_command(self):
        self.entry=Entry()
        self.entry.grid(column=1,row=self.row)
        self.entries.append(self.entry)
        self.row=self.row+1
    def save_button_command(self):
        self.total_entries_length=len(self.entries)
        print(self.total_entries_length)
        for widget in self.entries:
           self.Ordered_dictionary_for_entry_widget["Name"+str(self.current_widget+1)]=widget.get()
           self.current_widget=self.current_widget+1
        with open("example_fully_functional.txt","wb") as f:
            dill.dump(self.Ordered_dictionary_for_entry_widget,f)
    def Open_button_command(self):
        print("Total entries length",self.total_entries_length)
        with open("example_fully_functional.txt","rb") as f:
            self.Ordered_dictionary_for_entry_widget=dill.load(f)
            for key,values in self.Ordered_dictionary_for_entry_widget.items():
            self.values_for_entry_dictionary.append((values))  
            print(self.values_for_entry_dictionary)
        for i in (self.values_for_entry_dictionary):
            self.entry=Entry()
            self.entry.grid(column=1,row=i)
            self.entries.append(self.entry)
            print("Entry loaded",self.entries_loaded)
            #Insert the entries back into the UI
        [self.entries.insert(0,self.values_for_entry_dictionary) for
        self.entries,self.values_for_entry_dictionary in
        zip(self.entries,self.values_for_entry_dictionary)]

program = Program()

mainloop()

1 个答案:

答案 0 :(得分:1)

好的回答直接问题:self.row Open_button_command未在add_button_command中递增,因此当Entry尝试添加新的 for i in (self.values_for_entry_dictionary): self.entry=Entry() self.entry.grid(column=1,row=i) self.entries.append(self.entry) ## THIS ONE HERE ## self.row+=1 #####

时,它不准确
Open_button_command

我想建议一个更好的解决方案,然后跟踪变量中的下一列,但在我需要修改一些内容之前,首先在 for i in (self.values_for_entry_dictionary): self.entry=Entry() self.entry.grid(column=1,row=i) ...

.grid

您正在迭代需要插入条目而非索引的值,以获取要在range(len(X))中使用的索引,您可以使用 for i in range(len(self.values_for_entry_dictionary)): 代替:

enumerate

或更好地使用 for i,value in enumerate(self.values_for_entry_dictionary): self.entry=Entry() self.entry.grid(column=1,row=i) self.entry.insert(0,value) ... 制作条目并同时填写:

    [self.entries.insert(0,self.values_for_entry_dictionary) for
    self.entries,self.values_for_entry_dictionary in
    zip(self.entries,self.values_for_entry_dictionary)]

这样你不需要这个:

self.entries

在循环期间会覆盖self. self.values_for_entry_dictionaryenumerate,因此很多信息会在此期间搞乱,请改用self.entries

清理后,Entry将始终是窗口中所有self.row窗口小部件的列表,len(self.entries)应始终等于class Program: @property def row(self): return len(self.entries) ... ,这样就可以了更喜欢每次使用property来计算它:

self.row=X

然后注释掉任何试图设置setter的语句,因为您没有或需要self.row。每次使用add_button_command时,它都会使用属性进行计算,Open_button_command将始终在底部添加新条目。

然而,在Entry中,即使窗口中已经存在def Open_button_command(self): ... for i,value in enumerate(self.values_for_entry_dictionary): if i<len(self.entries): self.entry = self.entries[i] self.entry.delete(0,"end") #remove any previous content else: self.entry=Entry() self.entry.grid(column=1,row=i) self.entries.append(self.entry) # either way: self.entry.insert(0,value) ,您仍然在创建新的小部件,检查是否已经存在可以重用的小部件会更有意义:

self.values_for_entry_dictionary

虽然在打开文件时没有重置def Open_button_command(self): self.values_for_entry_dictionary=[] ... ,但如果你按两次打开按钮仍然会中断,所以文件中的值会添加到任何已经打开的条目中,所以最好这样做打开文件时也会重置它:

--Inside Function
 SELECT id FROM a_table WHERE name=(SUBSTRING(@configValue, @start, @end - @start))
    IF @@ROWCOUNT > 0
        RETURN 1;

如果您需要有关工作但有缺陷的代码的帮助,可以consider submitting it for review我很乐意提供其他提示,但我认为这足以至少让示例代码按预期工作。