tkinter python复选框问题

时间:2015-12-29 22:30:54

标签: python checkbox tkinter

这个python文件的目的是读取类似于下面的文件,修改具有" PL"的行。在形状领域。我遇到的问题是OK框正在流入初始文件选择按钮。此外,OK按钮不会显示在初始复选框中,也不会使用" PL"更新第一行。在里面。 一旦第一个复选框和文件选择框被Xed输出,该复选框似乎按照预期的第二行开启。 有人可以帮我解决这个问题吗? 它应该调出文件选择框,选择要打开的文件,选择要保存的文件。 此框应保持打开状态,复选框应打开。一旦你做出选择,它应该一次又一次地打开,直到没有更多的行具有" PL"在他们中。 新文件应该在正确的位置添加数据。

这是在阅读文件" 1288.kss"。将文件另存为" 1288r.kss"

D,88C200a,0,88C200a,88C200a,1,HSS,5x5x.375,A500B,4311.65,,S1E,,,,,,,,,,,,,,,,,,,
W,88C200a,0,COLUMN,07/23/15,SDS7.420,,,,,,,,,,,,,,,,,,,,,,,,,
M,88C200a,1,COLUMN,,,,,,,,,,,,,,,,,,,,,,,,,,,
S,2B,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,88C200a,0,88C200a,bs5_2,1,PL,1-1/4x13,A36,330.2,,,,,,,,,,,,,,,,,,,,,
D,88C200a,0,88C200a,p307,1,PL,3/8x9-5/16,A36,838.2,,,,,,,,,,,,,,,,,,,,,
D,88C200a,0,88C200a,p310,1,PL,3/8x7-1/4,A36,379.41,,,,,,,,,,,,,,,,,,,,,
D,88C200a,0,88C200a,p317,1,PL,3/8x6-5/8,A36,533.4,,,,,,,,,,,,,,,,,,,,,

这是代码:

from Tkinter import *
import Tkinter
import tkFileDialog


def main(root):
     fn = tkFileDialog.askopenfilename(master=root,
                                        initialdir=r'C:\kiss\Routing',
                                        filetypes=[("KSS", "*.kss")])
     if not fn: return
     fnFiltered = tkFileDialog.asksaveasfilename(master=root, 
                    initialdir=r'C:\kiss\Routing', 
                    filetypes=[("KSS", "*.kss")])
     if not fnFiltered: return 

     lines = open(fn).readlines()
     index0 = 0
     indexPage = 1
     index2 = 2
     indexDetail = 3
     # index 4 is the part number
     indexPart = 4
     indexQty = 5
     indexShape = 6 
     indexDescr = 7 
     # index 8 is the grade 
     indexGrade = 8 
     # length index, length is in millimeters - convert to inches and 16ths 
     # with function mm_to_imperial 
     indexLength = 9
     index10 = 10
     # index 11 is the remarks column 
     indexRemark = 11 
     # 1-1/2 | 1 | 15

     '''
     revisions:

     '''

     outputLines = [] 
     for i, line in enumerate(lines): 
          if "," in line or "*" in line: 
               lineList = line.strip().split(",")
               if lineList[0] == "L":
                    continue
               if lineList[0] == "A":
                    continue  
               if lineList[0] == "D":

                    if "PL" in lineList[indexShape]:
                         #
                         pn1 = lineList[indexPart]
                         #
                         def results(): 
                              top.destroy() 

                         top = Tkinter.Tk() 
                         CheckVar1 = Tkinter.IntVar() 
                         CheckVar2 = Tkinter.IntVar() 
                         CheckVar3 = Tkinter.IntVar()
                         CheckVar4 = Tkinter.IntVar()

                         CheckVar1.set(1) 
                         CheckVar2.set(0) 
                         CheckVar3.set(0)
                         CheckVar4.set(0)

                         C1 = Tkinter.Checkbutton(top, text = "Route 25 - Plate Table", 
                                                  variable=CheckVar1, height=1, width=20)
                         C2 = Tkinter.Checkbutton(top, text = "Route 35 - Forming", 
                                                  variable=CheckVar2, height=1, width=20) 
                         C3 = Tkinter.Checkbutton(top, text = "Route 175 - T-Load#1", 
                                                  variable=CheckVar3, height=1, width=20) 
                         C4 = Tkinter.Checkbutton(top, text = "Route 176 - T-Load#2", 
                                                  variable=CheckVar4, height=1, width=20) 
                         C1.pack() 
                         C2.pack() 
                         C3.pack()
                         C4.pack()
                         bt = Button(text='OK', command=lambda: top.destroy()) 
                         bt.pack(side='left') 
                         top.mainloop() 
                         cv1 = CheckVar1.get()
                         cv2 = CheckVar2.get()
                         cv3 = CheckVar3.get()
                         cv4 = CheckVar4.get()

                         #
                         if cv1 == 0:
                              rt1 = ""
                         if cv1 == 1:
                              rt1 = "25"
                         if cv2 == 1:
                              rt1 = rt1+"-35"
                         if cv3 == 1:
                              rt1 = rt1+"-175"                         
                         if cv4 == 1:
                              rt1 = rt1+"-176"

                         lineList[index10] = rt1


               outputLines.append(lineList)


     try: 
          f = open(fnFiltered, 'w') 
          f.write("\n".join([','.join(lineList) for lineList in outputLines])) 
          f.close() 
     except Exception, e: 
          print e 

     # print "\n".join([','.join(lineList) for lineList in outputLines]) 
root = Tkinter.Tk()
Tkinter.Button(root, text="Select File To Process", command=lambda: main(root)).pack()
Tkinter.Button(root, text="Exit", command=root.destroy).pack()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

在根窗口被销毁之后,您对CheckVar1.get()等的调用会发生,在这种情况下,变量也会被销毁。

在销毁根窗口之前,必须调用get方法来响应事件,而不是在根窗口被销毁之后立即调用。