CodeMonkey
CodeMonkey

Reputation: 1156

Python Tkinter Text/Scrollbar Widget not functioning as expected

I have a homework assignment that I almost have completed, but the scrollbar does not function as expected. While it works when I'm focused on the Text, it does not work when I try to move the scrollbar or use the arrows.

from tkinter import *

class LoanCalculator:
    def __init__(self):
        window = Tk()
        window.title("Loan Calculator")

        frame = Frame(window)
        frame.pack()

        Label(frame, text="Loan Amount").grid(row=1,column=1, sticky=W)
        self.loanAmmount = StringVar()
        self.entryLoan = Entry(frame, textvariable=self.loanAmmount, justify=RIGHT)
        Label(frame, text="Years").grid(row=1, column=3,sticky=W)
        self.years = StringVar()
        self.entryYears = Entry(frame, textvariable=self.years, justify=RIGHT)
        btCalc = Button(frame, text="Calculate Loan", command=self.Calculate)
        scrollbar = Scrollbar(frame)
        self.text = Text(frame, width=60, height=10,wrap=WORD, yscrollcommand=scrollbar.set)

        self.entryLoan.grid(row=1, column=2)
        self.entryYears.grid(row=1, column=4)
        btCalc.grid(row=1, column=5)
        self.text.grid(row=2,column=1,columnspan=4)
        scrollbar.grid(row=2, column=5, sticky="NSW")



        window.mainloop()

    def Calculate(self):
        self.text.delete("1.0", END)
        self.text.insert(END, "{0:<20s}{1:<20s}{2:<20s}".format("Interest Rate", "Monthly Payment", "Total Payment"))
        aIR = 5.0
        mIR = 0
        mP = 0
        tP = 0
        fLA = 0
        fYear = 0
        lA = 0
        year = 0
        textToOut = ""
        while aIR <= 8.0:
            fLA = self.loanAmmount.get()
            fYear = self.years.get()
            lA = int(fLA)
            year = int(fYear)
            mIR = aIR / 1200
            mP = lA * mIR / (1 - (pow(1 / (1 + mIR), year * 12)))
            tP = mP * year * 12
            textToOut = format(aIR, ">5.3f") + "%" + format(mP, "20.2f") + format(tP, "20.2f") + "\n"
            self.text.insert(END, textToOut)
            aIR += 1.0 / 8


LoanCalculator()

EDIT I've changed my question to drop the text portion, as I have figured a workaround for that.

Upvotes: 0

Views: 141

Answers (1)

farzad
farzad

Reputation: 8855

You need to configure the scrollbar to run an action (update the view of the text area) on scrolling. Add this line of code after you defined your text area.

scrollbar.config(command=self.text.yview)

So the code block of __init__ would be something like this:

def __init__(self):
    window = Tk()
    window.title("Loan Calculator")

    frame = Frame(window)
    frame.pack()

    Label(frame, text="Loan Amount").grid(row=1,column=1, sticky=W)
    self.loanAmmount = StringVar()
    self.entryLoan = Entry(frame, textvariable=self.loanAmmount, justify=RIGHT)
    Label(frame, text="Years").grid(row=1, column=3,sticky=W)
    self.years = StringVar()
    self.entryYears = Entry(frame, textvariable=self.years, justify=RIGHT)
    btCalc = Button(frame, text="Calculate Loan", command=self.Calculate)
    scrollbar = Scrollbar(frame)
    self.text = Text(frame, width=60, height=10,wrap=WORD, yscrollcommand=scrollbar.set)
    scrollbar.config(command=self.text.yview)

    self.entryLoan.grid(row=1, column=2)
    self.entryYears.grid(row=1, column=4)
    btCalc.grid(row=1, column=5)
    self.text.grid(row=2,column=1,columnspan=4)
    scrollbar.grid(row=2, column=5, sticky="NSW")

Upvotes: 2

Related Questions