#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Tkinter tweaking - nearly there


    Been useing some of the links given around the forums and been trying to figure out alot of the great advice. Im slowly getting there but haveing troubles.

    It currently looks like = http://i.stack.imgur.com/9Je7Q.jpg


    im wanting it to look like = http://i.stack.imgur.com/PMvxc.jpg


    >< wont let me post images.

    Below is my code for the thing.
    Code:
    from Tkinter import *
    import ttk
    
    """Varables"""
    FileIn = "Data.txt"             #File IN. Change Name/Path if file is not in same dir as program.
    FileOut = "summary.txt"         #File OUT. Change Name/Path if file is not in same dir as program.
    Year09 = []                     #2009 KM list
    Year10 = []                     #2010 KM list
    Year11 = []                     #2011 KM list
    column_names = ["Year","Weekly Average","Yearl Max"] #Headings for columns
    In09 =[2009]                   #2009 inputs lists
    In10=[2010]                     #2010 inputs lists
    In11=[2011]                     #2011 inputs lists
    treedata = [(),(),()]           #Tables matix
    
    """Functions/Widgets"""
    #Button Functions
    def Butn2009(): tree.insert(END,treedata)                           #all borken -.-       
    def Butn2010(): listbox.insert (END, "Will Display 2010 data")    
    def Butn2011(): listbox.insert (END, "Will Display 2011 data")
    def ButnAll():  tree.insert('', 'end', values =x)
    def Exit():
        fout = file(FileOut, "w")               #Open users file in write
        #fout.write(column_names)               #not working -.- 
        fout.write(In09)                        #Dump In09 List into file.
        fout.write(In10)                        #Dump In10.how to put on new line?
        fout.write(In11)                        #Dump In10 
        root.destroy()                          #Closes the program
    
    """Input Data"""
    #Try to load data file. 
    try:
        fin = open(FileIn, "r")                #Open File to read
    except:                                     #If file is not found put up error message.
        print "File Not Found"
        print "Please Check File Name & Path"
        pass                                    #Pass so program dosnt crash
    
    #Populate Lists
    Year09 = map(float, fin.readline().strip().split(","))      #Populate year lists.
    In09.append(round(sum(Year09)/52,2))                        #Append the average and
    In09.append(round(max(Year09),2))                           #total to InLists.
    Year10 = map(float, fin.readline().strip().split(","))      
    In10.append(round(sum(Year10)/52,2)) 
    In10.append(round(max(Year10),2))
    Year11 = map(float, fin.readline().strip().split(","))      
    In11.append(round(sum(Year11)/52,2)) 
    In11.append(round(max(Year11),2))
    treedata=[(In09),(In10),(In11)]             #Put Inlists into a matrix for table.
    fin.close()                                 #Close input file.
    
    """Start Program"""
    root = Tk()                                 #Make a Window.
    root.title("Yearly Temps")                  #Name Window.
    HeaderFrame = Frame(root)                   #Top title & menu buttons frame.   
    LowFrame = Frame(root)                      #Lower output frame
    
    """HeaderFrame"""
    #Top Title & Positioning
    Title = StringVar()                                                 #Define as a string
    label=Label(HeaderFrame,textvariable=Title,relief=RAISED,font=(10)) #Label in HeadFrame & tell to get.
    Title.set("Yearly Temp Breakdown")                                  #Text to display in Title Varable.
    label.pack(fill=BOTH, expand=1)                                     #Posistioning
    
    #Make button's, call relvant widget & posistion.
    Butn2009 = Button(HeaderFrame, text="2009", command = Butn2009)     #Make Button, put in HeaderFrame
    Butn2009.pack(side=LEFT)                                            #then Name Button & Call widget    
    Butn2010 = Button(HeaderFrame, text="2010", command = Butn2010)
    Butn2010.pack(side=LEFT)                                            #Pack butons next to each other
    Butn2011 = Button(HeaderFrame, text="2011", command = Butn2011)     #going left
    Butn2011.pack(side=LEFT)
    ButnAll = Button(HeaderFrame, text="All Years", command = ButnAll)
    ButnAll.pack(side=LEFT)
    ButnExit = Button(HeaderFrame, text="Save & Exit", command = Exit)
    ButnExit.pack(side=LEFT)
    
    #Padding line to right of buttons incase user resizes window
    label=Label(HeaderFrame,relief=RAISED)              #Label in HeadFrame & tell to get.
    label.pack(fill=BOTH, expand=1)                     #Posistioning
    
    """LowFrame"""
    #Table to display results
    OutputTable = Frame(LowFrame,height=1,bd=3)
    OutputTable.pack()
    tree = ttk.Treeview(OutputTable, columns = column_names)
    for x in treedata:
        tree.insert('', 'end', values =x)
    for col in column_names: 
        tree.heading(col, text = column_names)
    tree.pack()
    
    """Ending Program"""
    #Frame placement in Window
    HeaderFrame.pack(fill=X)
    LowFrame.pack(fill=BOTH, expand=1)
    root.mainloop()                                     #Capture Mouse & Keybord inputs
    To start with it just feels ubber bloated or something ><
    Also i cant figure out how to get the buttons to work, delete that left-hand column and get the headings set... among other things....

    issues im having are.

    1. Need to figure out how to make the buttons display the data. ie 2009 will show just 2009 data. default when start up to show All.
    2.Need to delete the empty lefthand column.
    3. Get Tables headings to display correctly.
    4. Save file in a readable manner when save/exit is selected. - I was using pickle and ye.. jibberish.

    hey thanks Lew
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Code:
    #Where you have
    for col in column_names:
        tree.heading(col, text = column_names)
    #use instead
    for col in column_names:
        tree.heading(col, text = col)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    #Where you have
    for col in column_names:
        tree.heading(col, text = column_names)
    #use instead
    for col in column_names:
        tree.heading(col, text = col)
    Great thanx that got that sorted.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Well, lost lew, you had several questions. I picked the low hanging fruit.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo