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

    Join Date
    Oct 2017
    Posts
    85
    Rep Power
    1

    [Python 2.7 + Tkinter] Calculator Scrollbars Inperfect at Place, Help Needed!


    Hey Python Programmers...

    I was try to create an simple "Num * Num" Calculator with Python 2.7 (writted on an older
    windows systen, called windows XP) and Tkinter...

    This is my script (writted in python 2.7, on an older computer):
    Code:
    from Tkinter import *
    
    root = Tk( )
    root.title("Auto Calculator")
    root.geometry("640x480")
    
    def Calculate():
        try:
            num1 = int(entryNum1.get())
            num2 = int(entryNum2.get())
    
            calc = num1 * num2
            listboxCalc.insert(END, str(calc))
            
            entryNum1.delete(0, END)
            entryNum2.delete(0, END)
            N1Var.set("Number1")
            N2Var.set("Number2")
    
        except:
            listboxErro.insert(END, str("Failed to Calculate..."))
            N1Var.set("Number1")
            N2Var.set("Number2")
            entryNum1.delete(0, END)
            entryNum2.delete(0, END)
            
        
        
    scrollbarCalc = Scrollbar(root)
    scrollbarCalc.pack(fill = Y, side = RIGHT)
    
    scrollbarErro = Scrollbar(root)
    scrollbarErro.pack(fill = Y, side = RIGHT)
    
    listboxErro = Listbox(root, background = "gray", yscrollcommand = scrollbarErro.set)
    listboxErro.pack(fill = X, side = TOP)
    
    listboxCalc = Listbox(root, background = "gray", yscrollcommand = scrollbarCalc.set)
    listboxCalc.pack(fill = X, side = TOP)
    
    N1Var = IntVar()
    N1Var.set("Number1")
    entryNum1 = Entry(root, background = "gray", textvariable = N1Var)
    entryNum1.pack(fill = X, side = TOP)
    
    N2Var = IntVar()
    N2Var.set("Number2")
    entryNum2 = Entry(root, background = "gray", textvariable = N2Var)
    entryNum2.pack(fill = X, side = TOP)
    
    button = Button(root, text = "Cauculate", foreground = "white", background = "black", activeforeground = "blue", activebackground = "red", command = Calculate)
    button.pack(fill = X, side = TOP)
    
    root.mainloop()
    So it works perfectly, but my two scrollbars (one for the errors and one for the calculations)
    will not be placed on the two same listboxes...

    If you don't know about this imperfections, run my script, than you see whats wrong with my
    two scrollbars...

    I want the follow:
    1) scrollbarCalc must my placed on the "fill = Y, side = RIGHT", but precision on listboxCalc
    2) And scrollbarErro must be placed on the "fill = Y, side = RIGHT", but precision on listboxErro

    But if you can see with run my simple test calculator, python and tkinter doesn't this...
    The two scrollbars are not to the current place...

    Can anyone help me to correct my code, thanks for help...,
    Jamie
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,972
    Rep Power
    510
    Frames solve this problem.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,972
    Rep Power
    510
    your IntVar s are useless because you use the Entry get method which you need to convert to int, but use the IntVar s to display text. Screwy.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,972
    Rep Power
    510
    Code:
    from Tkinter import *
    
    def reset():
        entryNum1.delete(0, END)
        entryNum2.delete(0, END)
        entryNum1.insert('0', "Number1")
        entryNum2.insert('0', "Number2")
    
    def Calculate():
        try:
            num1 = int(entryNum1.get())
            num2 = int(entryNum2.get())
    
            calc = num1 * num2
            listboxCalc.insert(END, str(calc))
            reset()
    
        except:
            listboxErro.insert(END, "Failed to Calculate...")
            reset()
        
    font=('fixed', 32)
    entry_options= dict(font=font,background='gray')
    
    root = Tk( )
    root.title("Auto Calculator")
    root.geometry("640x480")
    
    
    ################################################################
    f = Frame(root)
    f.pack(side=TOP, fill=X)
    
    scrollbarCalc = Scrollbar(f)
    scrollbarCalc.pack(fill = Y, side = RIGHT)
    
    listboxCalc = Listbox(f, background = "gray", yscrollcommand = scrollbarCalc.set, )
    listboxCalc.pack(fill = X, side = TOP)
    
    ################################################################
    f = Frame(root)
    f.pack(side=TOP, fill=X)
    
    scrollbarErro = Scrollbar(f)
    scrollbarErro.pack(fill = Y, side = RIGHT)
    
    listboxErro = Listbox(f, background = "gray", yscrollcommand = scrollbarErro.set, )
    listboxErro.pack(fill = X, side = TOP)
    
    
    entryNum1 = Entry(root, **entry_options)
    entryNum1.pack(fill = X, side = TOP)
    
    entryNum2 = Entry(root, **entry_options)
    entryNum2.pack(fill = X, side = TOP)
    
    reset()
    
    button = Button(root, text = "Cauculate",
                    foreground = "white", background = "black", activeforeground = "blue", activebackground = "red",
                    command = Calculate, font=font)
    
    button.pack(fill = X, side = TOP)
    
    root.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,972
    Rep Power
    510
    I was unable to drag the scroll bars with my pointer, but you've said the program works otherwise perfectly.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo