使用Tkinter将脚本转换为GUI

时间:2017-02-18 16:37:30

标签: python tkinter

我真的需要帮助,我编写了一个实现流程方程的脚本。问题是我希望将其转换为GUI,我是一名学生,我打算很快提交这项作业,我现在没有时间学习Tkinter,但我肯定会在下个月学习它。下面的脚本:

print ("this program measures gas flowrate in pipes with effect of elevation, making use of USCS unit")
e=2.718
Tb=520
Pb=14.7
f=float(input("Friction factor,f: "))
P1=float(input("upstream pressure,P1: "))
P2=float(input("downstream pressure,P2: "))
G=float(input("gas gravity,G: "))
Tf=float(input("average gas flowing temperature,Tf: "))
L=float(input("pipe line segment,L: "))
Z=float(input("gas compressibility factor at flowing temperature,Z: "))
D=float(input("pipe inside diameter,D: "))
H1=float(input("upstream elevation,H1: "))
H2=float(input("downstream elevation,H2: "))
s=float((0.0375*G)*((H2-H1)/(Tf*Z)))
j=float((e**s-1)/s)
Le=float(L*j)
F=float(2/f**0.5)
Q=38.77*F*(Tb/Pb)*((P1**2-(e**s*P2**2))/(G*Tf*Le*Z))**0.5*D**2.5
print(j);
print(s);
print(Q);

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

首先,导入tkinter并创建主对象:

import Tkinter as tk

master = tk.Tk()

然后创建将进入主窗口的输入。对于每个输入,为输入创建标签和条目:

示例:

L1 = tk.Label(master, text="friction factor, f: ")
L1.pack()
L1.grid(row=0, column=0)

E1 = tk.Entry(master, bd =5)
E1.pack()
E1.grid(row=0, column=1)

# .... all other labels and input entries
# And a label for the result:

result = tk.Label(master)
result.pack()

然后,将条目中的所有值都获取到变量,例如:

f = float(E1.get())

添加按钮进行计算:

button = tk.Button(master, text='Calculate', command=calculate)
button.pack()

# calculate is a function that you will define, that gets all values from the input and returns the final value. Send also the result label to the function to change the text of the result label.

def calculate(result, f, .....):
    # Your calculating algorithem
    output = "j: " + str(j) + ", Q: "+ str(Q) + ", s: "+str(s)
    result.config(text=output)

在代码的最后,您将拥有运行窗口的这一行:

master.mainloop()

如果您需要进一步说明,请写信。