[英]is there a way to use same variables in different functions without using global?
check1 = False
def adding():
global f_num, s_num, check1
check1 = True
e.delete(0, END)
f_num = e.get()
s_num = e.get()
e.delete(0, END)
def show_answer():
if check1:
e.insert(0,f_num+s_num)
I'm new to coding and using this website...I'm using tkinter module to build a calculator.我是编码和使用这个网站的新手......我正在使用 tkinter 模块来构建计算器。 when I set the
show_answer()
function in the equal sign button it was supposed to show the answer to the addition to the numbers.当我在等号按钮中设置
show_answer()
function 时,它应该显示数字加法的答案。
If you are using tkinter, then you can define it in your root.如果您使用的是 tkinter,那么您可以在根目录中定义它。 Like:
喜欢:
root.Variable = True
Here is an example:这是一个例子:
from tkinter import *
root = Tk()
root.Check1 = 0
def command():
print(root.Check1)
root.Check1 += 1
Button(text="Hello World", command=command).grid()
root.mainloop()
It keeps adding 1 to root.Check1
and you can access the variable normally inside and outside the functions.它不断将 1 添加到
root.Check1
并且您可以在函数内部和外部正常访问该变量。 You can use this concept.你可以使用这个概念。 Or stay with
globals
and use return
或者继续使用
globals
并使用return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.