Python 如何通过提交按钮获取输入,然后打印为文本?

Python 如何通过提交按钮获取输入,然后打印为文本?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,首先,为糟糕的代码感到抱歉,我是个笨蛋!然而,我正在修补一些tkinter,我几乎达到了我的代码目标。。。但是当按下按钮时,我似乎不知道如何从转换为文本框的输入字段中获取输入 代码似乎工作正常,因为我在终端中获得了所需的输出,而不是tkinter窗口。我试过使用普通文本框和滚动文本 import tkinter as tk from tkinter import scrolledtext fields = ('Principal', 'Expected Return', 'Number of

首先,为糟糕的代码感到抱歉,我是个笨蛋!然而,我正在修补一些tkinter,我几乎达到了我的代码目标。。。但是当按下按钮时,我似乎不知道如何从转换为文本框的输入字段中获取输入

代码似乎工作正常,因为我在终端中获得了所需的输出,而不是tkinter窗口。我试过使用普通文本框和滚动文本

import tkinter as tk
from tkinter import scrolledtext

fields = ('Principal', 'Expected Return', 'Number of Years', 'Yearly Payment', 'Cost Rate') #fields for gui

def some_logics(entry_from_user):
    #grab input from user from fields
    principal = int(entry_from_user['Principal'].get()) 
    rate_return = float(entry_from_user['Expected Return'].get()) /100
    number_periods = int(entry_from_user['Number of Years'].get())
    pmt = int(entry_from_user['Yearly Payment'].get())
    rate_cost = float(entry_from_user['Cost Rate'].get()) /100

    #main function, utilizes variables to calculcate and print out desired text
    def print_out():

        #investment return, initial investment, expected yearly return, number of years, yearly payment
        def accrued(principal, rate_return, number_periods, pmt=0):
            cash = principal
            for periods in range(number_periods):
                cash += (cash * rate_return) + pmt
            return cash

        #if capital is borrowed insert capital, cost of capital and number of years
        def cost_of_capital(principal, rate_cost, number_periods):
            effective_cost_rate = (1 + (rate_cost/12))**12 - 1
            return principal * effective_cost_rate * number_periods


        end_cash = accrued(principal, rate_return, number_periods, pmt) #calculate total end sum for investment
        cost_capital_end = cost_of_capital(principal, rate_cost, number_periods) #calculate total cost of capital
        print(end_cash)
        print(cost_capital_end)
        print(f'Initial investment was {principal}, while after the period cash is {end_cash}. This is a total gain of {end_cash - principal}')
        print(f'Of the total gain {pmt * number_periods} was coming from the installments. This results in a net gain of {end_cash - principal - (pmt * number_periods)}')
        print(f'Cost of investment is: {cost_capital_end} and this shows a actual gain of {(end_cash - principal - (pmt * number_periods)) - cost_capital_end}')

    return print_out()

def makeform(root, fields):
    #create a dictionary for fields
    entries = {} 
    for field in fields:
        print(field)
        row = tk.Frame(root)
        lab = tk.Label(row, width=22, text=field+": ", anchor='w')
        ent = tk.Entry(row)
        ent.insert(0, "0")
        row.pack(side = tk.TOP, fill = tk.X, padx = 5, pady = 5)
        lab.pack(side = tk.LEFT)
        ent.pack(side = tk.RIGHT, expand = tk.YES, fill = tk.X)
        entries[field] = ent
    return entries

if __name__ == '__main__':
    root = tk.Tk() #initialize object
    ents = makeform(root, fields) #create form
    #create a button, button is placing user input into funcion
    b1 = tk.Button(root, text='Calculate',
        command = (lambda e=ents: some_logics(e)))
    b1.pack(side=tk.LEFT, padx = 5, pady = 5)
    #create a quit program button
    b2 = tk.Button(root, text='Quit', command=root.quit)
    b2.pack(side=tk.RIGHT, padx=5, pady=5)
    #create a textfield which returns user input in format from print_out function
    T = scrolledtext.ScrolledText(root, state='disabled')
    T.pack(side= tk.BOTTOM, padx=5, pady=5)
    T.insert(tk.END, (lambda e=ents: some_logics(e)))
    root.mainloop()

附加问题:我将代码重新构造为嵌套函数,这是pythonic还是noise?

我认为嵌套函数有噪声,是的

我在这里所做的改变是:

  • 用黑色自动格式化
  • 将主逻辑包装在
    main()
    函数中,以避免使用全局变量
  • 将嵌套函数取消嵌套到自由函数中(这些函数实际上属于一个模块)
  • 将输出生成函数重构为自由函数
  • 使
    update\u form
    (née
    some\u logics
    )函数接受它应该更新的输出字段(并实际更新)
  • 修正了lambdas的一些非正统用法
希望这有帮助

import tkinter as tk
from tkinter import scrolledtext

fields = (
    "Principal",
    "Expected Return",
    "Number of Years",
    "Yearly Payment",
    "Cost Rate",
)  # fields for gui


# investment return, initial investment, expected yearly return, number of years, yearly payment
def accrued(principal, rate_return, number_periods, pmt=0):
    cash = principal
    for periods in range(number_periods):
        cash += (cash * rate_return) + pmt
    return cash


# if capital is borrowed insert capital, cost of capital and number of years
def cost_of_capital(principal, rate_cost, number_periods):
    effective_cost_rate = (1 + (rate_cost / 12)) ** 12 - 1
    return principal * effective_cost_rate * number_periods


def calculate_output(number_periods, pmt, principal, rate_cost, rate_return):
    end_cash = accrued(
        principal, rate_return, number_periods, pmt
    )  # calculate total end sum for investment
    cost_capital_end = cost_of_capital(
        principal, rate_cost, number_periods
    )  # calculate total cost of capital
    output = "\n".join(
        (
            f"Initial investment was {principal}, while after the period cash is {end_cash}. This is a total gain of {end_cash - principal}",
            f"Of the total gain {pmt * number_periods} was coming from the installments. This results in a net gain of {end_cash - principal - (pmt * number_periods)}",
            f"Cost of investment is: {cost_capital_end} and this shows a actual gain of {(end_cash - principal - (pmt * number_periods)) - cost_capital_end}",
        )
    )
    return output


def update_form(entry_from_user, output_field):
    # grab input from user from fields
    principal = int(entry_from_user["Principal"].get())
    rate_return = float(entry_from_user["Expected Return"].get()) / 100
    number_periods = int(entry_from_user["Number of Years"].get())
    pmt = int(entry_from_user["Yearly Payment"].get())
    rate_cost = float(entry_from_user["Cost Rate"].get()) / 100

    output = calculate_output(number_periods, pmt, principal, rate_cost, rate_return)
    output_field.delete("1.0", tk.END)  # clear
    output_field.insert(tk.INSERT, output)


def makeform(root, fields):
    # create a dictionary for fields
    entries = {}
    for field in fields:
        row = tk.Frame(root)
        lab = tk.Label(row, width=22, text=field + ": ", anchor="w")
        ent = tk.Entry(row)
        ent.insert(0, "0")
        row.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
        lab.pack(side=tk.LEFT)
        ent.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.X)
        entries[field] = ent
    return entries


def main():
    root = tk.Tk()  # initialize object

    output_field = scrolledtext.ScrolledText(root)  # create output field

    ents = makeform(root, fields)  # create form
    # create a button, button is placing user input into funcion
    b1 = tk.Button(
        root, text="Calculate", command=lambda: update_form(ents, output_field)
    )
    b1.pack(side=tk.LEFT, padx=5, pady=5)
    # create a quit program button
    b2 = tk.Button(root, text="Quit", command=root.quit)
    b2.pack(side=tk.RIGHT, padx=5, pady=5)
    # create a textfield which returns user input in format from print_out function
    output_field.pack(side=tk.BOTTOM, padx=5, pady=5)
    root.mainloop()


if __name__ == "__main__":
    main()

谢谢这很有帮助!也将停止使用嵌套函数,我想这是Excel的坏习惯。很高兴我能提供帮助。不使用嵌套函数的一个很好的原因是,一旦开始编写测试,单元的可测试性也会更容易。