Python 如何使用tk.Entry中的变量,将tkinter按钮与函数一起使用?
我正试图编写一些简单的代码,将变量放入一个简单的函数中,该函数使用温度和压力来计算一个名为fO2的输出 我看不出如何更新输入变量,现在它使用0。Python 如何使用tk.Entry中的变量,将tkinter按钮与函数一起使用?,python,tkinter,Python,Tkinter,我正试图编写一些简单的代码,将变量放入一个简单的函数中,该函数使用温度和压力来计算一个名为fO2的输出 我看不出如何更新输入变量,现在它使用0。 此外,无论您在Temp\u input中输入什么,都会在P\u input中复制 from matplotlib.backends.backend_tkagg import * from matplotlib.backend_bases import key_press_handler from matplotlib.figure import Fig
此外,无论您在
Temp\u input
中输入什么,都会在P\u input
中复制
from matplotlib.backends.backend_tkagg import *
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import tkinter as tk
root = tk.Tk()
root.title('Oxygen Fugacity')
root.geometry("500x400")
Temp_input = tk.Entry(root, width=15, borderwidth=5,textvariable=Tcinput)
Temp_input.grid(row=1,column=1)
P_input = tk.Entry(root, width=15, borderwidth=5,textvariable=Pinput)
P_input.grid(row=2,column=1)
Templabel = tk.Label(root,text='Temperature (C)').grid(row=1,column=0)
Plabel = tk.Label(root,text='Pressure (bar)').grid(row=2,column=0)
comp = tk.Button(root, text='Compute', command=lambda: all).grid(row=3,column=1)
Tcinput = tk.IntVar(master=root,value=Tcinput).get()
Pinput = tk.IntVar(master=root,value=Pinput).get()
tk.Label(root, text='NNO').grid(row=1,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='FMQ').grid(row=2,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='IW').grid(row=3,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='MH').grid(row=4,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='CoCoO').grid(row=5,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='CCO').grid(row=6,column=3,padx=(30,0),pady=(10,10))
NNO_OUT = tk.Message(root, text = all(Tcinput,Pinput)[0]).grid(row=1,column=4,padx=(30,0))
FMQ_OUT = tk.Message(root, text = all(Tcinput,Pinput)[1]).grid(row=2,column=4,padx=(30,0))
IW_OUT = tk.Message(root, text = all(Tcinput,Pinput)[2]).grid(row=3,column=4,padx=(30,0))
MH_OUT = tk.Message(root, text = all(Tcinput,Pinput)[3]).grid(row=4,column=4,padx=(30,0))
CoCoO_OUT = tk.Message(root, text = all(Tcinput,Pinput)[4]).grid(row=5,column=4,padx=(30,0))
CCO_OUT = tk.Message(root, text = all(Tcinput,Pinput)[5]).grid(row=6,column=4,padx=(30,0))
root.mainloop()
def all(Tc=1200,P=1):
a = str(round(fo2.NNO(Tc,P),2))
b = str(round(fo2.FMQ(Tc,P),4))
c = str(round(fo2.IW(Tc,P),4))
d = str(round(fo2.MH(Tc,P),4))
e = str(round(fo2.CoCoO(Tc,P),4))
f = str(round(fo2.CCO(Tc,P),4))
x=[a,b,c,d,e,f]
return x
编辑
我编辑了代码,但按钮仍然不起作用,我尝试在函数中使用lambda和()
有人能看到问题吗
from matplotlib.backends.backend_tkagg import *
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import tkinter as tk
def some_func():
global Tcinput, Pinput
Tc = Tcinput.get() # GETTER
P = Pinput.get() # GETTER
a = str(round(fo2.NNO(Tc,P),4))
b = str(round(fo2.FMQ(Tc,P),4))
c = str(round(fo2.IW(Tc,P),4))
d = str(round(fo2.MH(Tc,P),4))
e = str(round(fo2.CoCoO(Tc,P),4))
f = str(round(fo2.CCO(Tc,P),4))
x=[a,b,c,d,e,f]
return x
root = tk.Tk()
root.title('Oxygen Fugacity')
root.geometry("500x400")
Tcinput = tk.IntVar(master=root,value=1200) # NEW
Pinput = tk.IntVar(master=root,value=1) # NEW
Temp_input = tk.Entry(root, width=15, borderwidth=5,textvariable=Tcinput)
Temp_input.grid(row=1,column=1)
P_input = tk.Entry(root, width=15, borderwidth=5,textvariable=Pinput)
P_input.grid(row=2,column=1)
Templabel = tk.Label(root,text='Temperature (C)').grid(row=1,column=0)
Plabel = tk.Label(root,text='Pressure (bar)').grid(row=2,column=0)
tk.Label(root, text='NNO').grid(row=1,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='FMQ').grid(row=2,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='IW').grid(row=3,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='MH').grid(row=4,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='CoCoO').grid(row=5,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='CCO').grid(row=6,column=3,padx=(30,0),pady=(10,10))
comp = tk.Button(root, text='Compute', command=lambda: some_func()).grid(row=3,column=1)
NNO_OUT = tk.Message(root, text = some_func()[0]).grid(row=1,column=4,padx=(30,0))
FMQ_OUT = tk.Message(root, text = some_func()[1]).grid(row=2,column=4,padx=(30,0))
IW_OUT = tk.Message(root, text = some_func()[2]).grid(row=3,column=4,padx=(30,0))
MH_OUT = tk.Message(root, text = some_func()[3]).grid(row=4,column=4,padx=(30,0))
CoCoO_OUT = tk.Message(root, text = some_func()[4]).grid(row=5,column=4,padx=(30,0))
CCO_OUT = tk.Message(root, text = some_func()[5]).grid(row=6,column=4,padx=(30,0))
root.mainloop()
根据我从代码中了解到的情况,您需要获取保存Tc和p值的IntVar。以下是新代码:
from matplotlib.backends.backend_tkagg import *
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import tkinter as tk
root = tk.Tk()
root.title('Oxygen Fugacity')
root.geometry("500x400")
Tcinput = IntVar(master=root,value=1200) # NEW
Pinput = IntVar(master=root,value=1) # NEW
def some_func():
global Tcinput, Pinput
Tc = Tcinput.get() # GETTER
P = Pinput.get() # GETTER
a = str(round(fo2.NNO(Tc,P),2))
b = str(round(fo2.FMQ(Tc,P),4))
c = str(round(fo2.IW(Tc,P),4))
d = str(round(fo2.MH(Tc,P),4))
e = str(round(fo2.CoCoO(Tc,P),4))
f = str(round(fo2.CCO(Tc,P),4))
x=[a,b,c,d,e,f]
return x
Temp_input = tk.Entry(root, width=15, borderwidth=5,textvariable=Tcinput)
Temp_input.grid(row=1,column=1)
P_input = tk.Entry(root, width=15, borderwidth=5,textvariable=Pinput)
P_input.grid(row=2,column=1)
Templabel = tk.Label(root,text='Temperature (C)').grid(row=1,column=0)
Plabel = tk.Label(root,text='Pressure (bar)').grid(row=2,column=0)
comp = tk.Button(root, text='Compute', command=lambda: some_func()).grid(row=3,column=1)
tk.Label(root, text='NNO').grid(row=1,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='FMQ').grid(row=2,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='IW').grid(row=3,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='MH').grid(row=4,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='CoCoO').grid(row=5,column=3,padx=(30,0),pady=(10,10))
tk.Label(root, text='CCO').grid(row=6,column=3,padx=(30,0),pady=(10,10))
NNO_OUT = tk.Message(root, text = some_func()[0]).grid(row=1,column=4,padx=(30,0))
FMQ_OUT = tk.Message(root, text = some_func()[1]).grid(row=2,column=4,padx=(30,0))
IW_OUT = tk.Message(root, text = some_func()[2]).grid(row=3,column=4,padx=(30,0))
MH_OUT = tk.Message(root, text = some_func()[3]).grid(row=4,column=4,padx=(30,0))
CoCoO_OUT = tk.Message(root, text = some_func()[4]).grid(row=5,column=4,padx=(30,0))
CCO_OUT = tk.Message(root, text = some_func()[5]).grid(row=6,column=4,padx=(30,0))
root.mainloop()
将
all
的定义移到顶部(就在导入之后),并使用正确的参数将command=lambda:all
更改为command=lambda:all(…)
,说明grid()
方法始终返回None
,因此,这是在调用mainloop()
之前立即分配给所有*\u OUT
变量的值。在所有tk.Message
中使用all
后,您还定义了all
。这意味着它将使用python内置的all
函数,而不是您定义的函数……这就是为什么您应该避免将自己的函数命名为与内置函数相同的函数。@Lizzard已经将some_func定义向上移动,但按钮仍然不起作用,我已经尝试使用lambda,并在函数上使用(),这个问题的一些答案表明了这一点。还有什么想法吗?再次欢呼:)你知道按钮不会做任何正确的事情吗?而且all(Tcinput,Pinput)
不会使用你定义的all
功能。相反,它将使用python内置的all
函数。没错,我调整了解决方案。嘿@TheLizzard,这可能是个愚蠢的问题。我怎样才能使按钮工作?你说得对,按钮的作用很好:(@EdwardBaker有几个愚蠢的问题,而这不是其中之一。在这个答案中,代码被困在root.mainloop()中)
因此函数some_func
永远不会被定义为python的代码执行没有到达代码的那一部分。在import语句之后移动函数定义,按钮应该可以工作