【发布时间】:2018-02-12 20:35:46
【问题描述】:
更新:我现在明白这实际上只是关于范围与导入的一些混淆。我正在更新全局值,但找不到从另一个“求和函数”文件返回多个值的方法。现在,我知道这可以很容易地使用 Alex 指出的元组来完成。
所以,这是我写的一些代码。正如您可能看到的那样,我的数学函数并没有真正采用任何参数。我正在尝试将其更改为 Sum、Subtraction 和类似的函数需要两个参数,而像 sin 这样的函数只需要一个参数。现在我想问的是,我怎样才能实现给它参数 n 并返回答案像我在这里所做的那样更新全局值。任何和所有的帮助将不胜感激
from tkinter import *
import math
root= Tk()
num1=StringVar()
txtDisplay = Entry(root, textvariable = num1, width=17, font='Arial 25',justify="right");
txtDisplay.focus();
txtDisplay.grid(columnspan=5,row=0,ipady=8,padx=18,pady=10)
a=0
common=''
condition=0
oneButton = Button(root, text="1", width='5',command = lambda: clck(1 ))
oneButton.grid(row=6, column=1, ipady=8, ipadx=8)
twoButton = Button(root, text="2", width='5',command = lambda: clck(2))
twoButton.grid(row=6, column=2, ipady=8, ipadx=8)
addButton = Button(root, text="+", width='5',command = lambda: addition() )
addButton.grid(row=7, column=4, ipady=8, ipadx=8, padx=(0, 11))
subButton = Button(root, text="-", width='5',command = lambda: subtraction() )
subButton.grid(row=8, column=4, ipady=8, ipadx=8, padx=(0, 11))
sinButton = Button(root, text="sin", width='5',command = lambda: sin() )
sinButton.grid(row=9, column=4, ipady=8, ipadx=8, padx=(0, 11))
def clck (number):
global common
common+= str(number)
num1.set(common)
def sin():
global common
global a
a = math.sin(int(a))
num1.set(a)
def addition():
global a
global common
try:
a=a+ int(common)
except:
pass
#print(a)
num1.set(a)
common=''
global condition
condition='add'
def subtraction():
global a
global common
a=a- int(common)
#print(a)
num1.set(a)
common=''
root.mainloop()
另外,我现在知道我只实现了几个按钮。如果有任何方法可以改进我的代码,也请告诉我,如果有什么你没有得到的。
【问题讨论】:
-
但是,我的程序并没有真正将两个输入作为参数。它将全局值设置为输入值,然后处理这些全局值,而不是将它们作为参数传递给函数。确实那一点信息有帮助吗?
标签: python function tkinter global-variables