Python Tkinter文本/滚动条小部件无法按预期运行

时间:2014-10-27 00:27:25

标签: python user-interface tkinter

我有一项几乎已经完成的作业,但滚动条无法按预期运行。虽然当我专注于文本时它会起作用,但当我尝试移动滚动条或使用箭头时,它不起作用。

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()

修改 我已经改变了我的问题以删除文本部分,因为我已经找到了解决方法。

1 个答案:

答案 0 :(得分:2)

您需要配置滚动条以在滚动时运行操作(更新文本区域的视图)。在定义文本区域后添加此行代码。

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

因此__init__的代码块将是这样的:

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")