简体   繁体   English

用户输入上的 Python3 Tkinter 名称错误(未定义)

[英]Python3 Tkinter name error (not defined) on user input

I'm getting the following error:我收到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/XXXXXXXXXXXXX/Desktop/Python/Test Py/TestGUI.py", line 37, in trans1
    print(enter_principal)
NameError: name 'enter_principal' is not defined

I'm currently trying to learn python, so I'd be lying if I said I had any idea on what is going wrong.我目前正在尝试学习 python,所以如果我说我对出了什么问题有任何想法,那我就是在撒谎。 Here is my source code, trying to make a basic compound interest calculator.这是我的源代码,试图制作一个基本的复利计算器。 Getting this error when I'm trying to get an input from the user.当我尝试从用户那里获取输入时出现此错误。 Code:代码:

#Importing GUI Module
import tkinter as tk
from tkinter import *

#Creating window
root = tk.Tk()

####Functions#####

#Root screen exit button
def exitroot():
    root.destroy()
    
#principal input
def principal():
    
    #Creating principal window and destroying home window
    window = tk.Tk()
    exitroot()
    
    #Creating widgets
    title_principal = tk.Label(window, text='Please enter your pricipal value: ')
    enter_principal = tk.Entry(window)
    b1 = tk.Button(window, text='Submit', command=trans1)

    title_principal.grid()
    enter_principal.grid()
    b1.grid()

def trans1():
    #temp function for testing purposes
    print(enter_principal)

####    

#CREATING HOME WINDOW WIDGETS
title_main = tk.Label(root, text="Compound Intrest Calculator", font=("Arial", 20, 'bold'))
start_button = tk.Button(root, text="Start", width='6', height='2', command=principal)
exit_button = tk.Button(root, text="Exit", width='6', height='2', command=exitroot)
credits_main = tk.Label(root, text="M.CXXXXXXXX 2020", font=("Arial", 8))

#PACKING HOME WINDOW WIDGETS VIA GRID
title_main.grid(row='0', columnspan='2')
start_button.grid(row='1', column='0')
exit_button.grid(row='1', column='1')
credits_main.grid(row='2', columnspan='2')

root.mainloop()

Any help is greatly appreciated.任何帮助是极大的赞赏。 I apologise if my code is hard to follow or has blantant errors.如果我的代码难以理解或有明显错误,我深表歉意。 I've spent some time looking for a fix but I am really struggling as none have worked.我花了一些时间寻找解决方案,但我真的很挣扎,因为没有一个有效。

You need to change你需要改变

b1 = tk.Button(window, text='Submit', command=trans1)

TO:至:

b1 = tk.Button(window, text='Submit', command=lambda: trans1(enter_principal.get()))

The reason being is because you need to pass in the value typed into the tk.Entry by using enter_principal.get() .原因是您需要使用enter_principal.get()将键入的值传入tk.Entry

Lambda allows the function to be called only when the button is pressed.(since the command contains parenthesis and so would be called automatically) Lambda允许仅在按下按钮时调用 function。(因为命令包含括号,因此会自动调用)

Once you've passed this in, you can then pass it into the trans1 function and print it.一旦你把它传递进去,你就可以把它传递给trans1 function 并打印出来。

def trans1(answer):
    # temp function for testing purposes
    print(answer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM