2014-01-11 40 views
0

我已经创建了正常的gui程序,该程序读取文本文件中的行并显示在文本tk输入字段中。我想在按下按钮时自动增加行的索引,我对while循环感到困惑。代码我到目前为止所做的:自动增加文本文件的行

def forms(self): 

     b = tk.Button(bd ='4', text="Auto Fill", width = 20, command = self.autosave) 
     b.place (x=230, y=600) 

     c = tk.Button(bd ='4', text="Clear", width = 20, command = self.clear) 
     c.place (x=390, y=600) 

     d = tk.Button(bd ='4', text="Exit", width = 20, command = self.close) 
     d.place (x=550, y=600) 

     #Form Feilds Starts from Here: 
     self.date = tk.Label(font=('Arial', 13,'bold'), text = "Date: ",bg='white') 
     self.date.place(x=10,y=50) 

     self.ent_date = tk.Entry(bd='4',width='23') 
     self.ent_date.place(x=60, y=50) 


def autosave(self): 
     a = 0 
     fp = open('image.txt') 
     s = fp.readlines() 
     line = s[a] 
     self.ent_date.insert(0, line[0]) 
     box.showinfo('Success','Saved Successfully') 
     while true: 
      a += 1 

上面的代码使我的程序冻结。每次点击自动填充按钮时,如何使“a”的值增加? 在此先感谢。

回答

2

的代码加载上的每个按钮单击该文件,这是非常低效的。如何沿着以下几行:

def __init__(self): 
    fp = open('image.txt') 
    s = fp.readlines() 
    self.a = 0 
    self.line = s[a] 
    self.ent_date.insert(0, line[0])  

def forms(self): 
    b = tk.Button(bd ='4', text="Auto Fill", width = 20, command = self.autosave) 
    ... 

def autosave(self): 
    # save the current state, e.g., 
    # self.ent_date.insert(0, line[0]) - we'll leave it to the OP 
    box.showinfo('Success','Saved Successfully') 
    self.a += 1 
+0

....好点。 – starrify