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

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Tkinter GUI issues in Python 2.7.3


    Hi

    Im wanting to make a small python program for yearly temperatures. I can get nearly all working in the standard console but im wanting to implement it into a GUI.
    (Part that inst working is writing the summary to a txt file but i think i can sort this)

    the program opens a csv file reads it into lists. works out Average, Min & max temps. then on closing the application will save a summary to a new text file.

    Wanting the default start up screen to show All Years. when a button is clicked it just shows that years data.

    So in the end i want the program to look like the following:


    http://i50.tinypic.com/vgl7o6.jpg

    I can make up the buttons up top fine with:
    Code:
    class App:
    
        def __init__(self, master):
    
            frame = Frame(master)
            frame.pack()
    
            self.hi_there = Button(frame, text="All Years", command=self.All)
            self.hi_there.pack(side=LEFT)
    
            self.hi_there = Button(frame, text="2011", command=self.Y1)
            self.hi_there.pack(side=LEFT)
    
            self.hi_there = Button(frame, text="2012", command=self.Y2)
            self.hi_there.pack(side=LEFT)
    
            self.hi_there = Button(frame, text="2013", command=self.Y3)
            self.hi_there.pack(side=LEFT)
    
            self.hi_there = Button(frame, text="Save & Exit", command=self.Exit)
            self.hi_there.pack(side=LEFT)
    im not sure as to how to make the other elements.
    the title & table.

    I was going to post the code of the small program but decided not to. Once I have the structure/framework I think I can populate the fields.

    Thanks Lew

    >< not sure why i cant get my image of the layout to show. i put it in the insert image thing and shows in preview.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,886
    Rep Power
    481
    Storing all those buttons in
    self.hi_there is useless since each overwrites the previous.

    Maybe you could make a Frame in the frame, pack it at the bottom. Into that frame use the grid manager to pack labels into the layout you like. Use fill=BOTH.

    I've recently been using this online book. It's mostly useful.
    [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
    Storing all those buttons in
    self.hi_there is useless since each overwrites the previous.

    Maybe you could make a Frame in the frame, pack it at the bottom. Into that frame use the grid manager to pack labels into the layout you like. Use fill=BOTH.

    I've recently been using URL. It's mostly useful.
    hey thanks for getting back so fast.

    been reading over that site and to tell the truth im starting to get a lil lost ><

    Have fixed the button naming - thanx for pointing that out.

    Hey this sounds a lil checky but im doing my head in here.
    could i bet say the code to make up two buttons. then below the word Total statically then next to that the answer that changes with a button push.

    that way i should see the basic code of the structure of how to add the other elements and I can then go on and rework them to suit what im doing.

    hey thanx again and i will keep reading over what you posted.

    lew
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,886
    Rep Power
    481
    Code:
    try:
        import Tkinter as tkinter
    except ImportError:
        import tkinter
    
    class Click:
    
        def __init__(self,sb,ID):
            self.sb = sb
            self.ID = ID                      # identify which button was pressed
            self.count = 0
    
        def __call__(self):                   # action on button press
            sb = self.sb
            v = sb.Vars[self.ID]
            t = v.get()
            if self.count%3 == 0:
                t = t.upper()
            elif self.count%3 == 1:
                t = t.lower()
            else:
                t = t.capitalize()
            v.set(t)
            self.count += 1
    
    class SillyButtons:
    
        def __init__(self):
            tk = self.tk = tkinter.Tk()
            self.Vars = dict()
    
            ID = 0
            v = self.Vars[ID] = tkinter.StringVar()
            v.set('Watch!')
            tkinter.Label(tk,textvariable=v).pack(side=tkinter.TOP)
            tkinter.Button(tk,text='click',command=Click(sb=self,ID=ID)).pack(side=tkinter.TOP)
    
            #ID = 1
            #...
    
            tkinter.Button(tk,text='exit',command=self.tk.destroy).pack(side=tkinter.TOP)
    
        def __call__(self):
             self.tk.mainloop()
    
    SillyButtons()()
    I don't understand your English well, and I hope this example is simple enough. Good luck! This program makes a small window (9 cm squared) window which may be hard to find.
    Last edited by b49P23TIvg; November 8th, 2012 at 10:08 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo