如何使用用户输入通过Tkinter写入输出?

时间:2018-06-27 22:21:19

标签: python tkinter

我试图创建代码来创建带有按钮的GUI计算器,并且尝试查找Tkinter帮助,但无法获得帮助。如何合并底部的注释掉部分以显示所显示数字的值?请随时向我的代码中添加建议。

编辑:我按照@abarnert的建议修复了代码,但是当我运行代码时,它总是写无效操作。我不知道该如何解决。有谁有更好的主意来修复我的代码?我只需要开始操作即可。我编辑的代码如下。

from math import sqrt
from tkinter import *
window = Tk()
window.title("Welcome to Calculator ")
window.configure(background = "white")
Label (window, text = "Calculator", bg = "white") .grid(row = 0, column = 
0, sticky = N)



#click function
def click():
    n = n_textentry.get()
    m = m_textentry.get()
    operation = operation_textentry.get()
    if operation == 1:
        print(n + m)

    elif operation == 2:
        print(n - m)

    elif operation == 3:
        print(n * m)

    elif operation == 4:
        print(n / m)

    elif operation == 5:
        print(n ** m)

    elif operation == 6:
        print(sqrt(n))

    else:
        print("Invalid Operation ")




#to create the box for the first number and store it
Label (window, text = "Enter the first number", bg = "white") .grid(row = 
1, column = 0, sticky = N)
n_textentry = Entry(window, width = 10, bg = "white")
n_textentry.grid(row = 2, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 3, 
column = 0, sticky = N)

#to create the box for the second number
Label (window, text = "Enter the second number", bg = "white") .grid(row = 
5, column = 0, sticky = N)
m_textentry = Entry(window, width = 10, bg = "white")
m_textentry.grid(row = 6, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 7, 
column = 0, sticky = N)

#to show list of options
Label (window, text = '''               Enter 1 for addition
                    Enter 2 for subtraction
                         Enter 3 for multiplication
              Enter 4 for division
                        Enter 5 for exponentiation

Enter 6 for square root *This will only work for 1st choice*''', bg = 
"white") .grid(row = 9, column = 0, sticky = W)

operation_textentry = Entry(window, width = 10, bg = "white")
operation_textentry.grid(row = 10, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 11, 
column = 0, sticky = N)

1 个答案:

答案 0 :(得分:3)

在GUI程序中,一旦GUI启动并运行,您只能在事件处理程序的回调中运行代码。这称为event-driven programming,一开始可能需要一段时间才能习惯。

例如,当用户单击第一个Submit按钮时,它将调用您的函数click。在该函数内部,您可以在那里执行任何适当的操作。

您已经使所有三个按钮都调用了相同的click函数。在这里没有太大意义。

实际上,您为什么还要三个单独的按钮?考虑一下典型GUI中的典型形式-有很多Entry字段,然后是用户在填写所有字段后单击的单个“ Submit”按钮。因此,让我们仅废弃前两个按钮,最后只剩下一个。

现在,在该click函数中,您可以“做任何适当的事情”。但是这里合适吗?

您需要做的第一件事是从三个Entry框中获取值。但是您试图将所有三个存储在同一变量中,因此您无法访问所有三个,只能访问最后一个。因此,与其重复三遍:

textentry = Entry(window, width = 10, bg = "white")

...给他们所有不同的名字:

n_entry = Entry(window, width = 10, bg = "white")
# ...
m_entry = Entry(window, width = 10, bg = "white")
# ...
operation_entry = Entry(window, width = 10, bg = "white")

现在,您的click函数可以获取所有三个值:

def click():
    n = n_entry.get()
    m = m_entry.get()
    operation = operation_entry.get()

当然,这些将是字符串,就像您在命令行应用程序中调用input()所得到的一样。因此,您必须在此处做同样的事情-呼叫intfloat,处理错误等。

但是,一旦完成,click的其余部分就可以成为您现有的注释掉的代码。 (除了您必须摆脱那些break语句之外,这里没有循环可以打破了。)

    if operation == 1:
        print(n + m)
    elif operation == 2:
        print(n - m)
    # etc.

但是,您可能希望更改这些print调用,以将结果显示在GUI中。例如,您可能有一个名为Label的空results_label,并将其文本配置为str(n+m),等等。