【问题标题】:Converting a script into GUI using Tkinter使用 Tkinter 将脚本转换为 GUI
【发布时间】:2017-02-18 16:37:30
【问题描述】:

我真的需要帮助,我编写了一个实现流量方程的脚本。问题是我很想把它转换成 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);

非常感谢您的帮助

【问题讨论】:

  • 您希望拥有什么样的 GUI?输入输出?
  • 听起来你可以使用EasyGUI
  • 那么,您是在要求我们为您做作业吗?这不是 stackoverflow 的用途。
  • i don't have time for learning Tkinter now but i will surely learn it next month 这是学习编程课程(或大多数课程)的一种非常糟糕的方法。
  • 谢谢大家。不是我不想学,我还是在努力每天学习python第一..

标签: python tkinter


【解决方案1】:

首先,导入 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()

如果您需要进一步解释,请写。

【讨论】:

  • PEP8 建议不要导入通配符。
  • @BryanOakley 我知道,这是一个坏习惯,但他并不是真的在寻找最好的解决方案——只是一个可行的解决方案。
  • 如果你知道这是一个坏习惯,就不要教别人这样做。这是一个对 tkinter 一无所知的人,你要做的第一件事就是教他们一个坏习惯。
  • @BryanOakley 你是对的,我正在接受你的建议。
  • 感谢您的帮助,我刚买了一本书来学习 Tkinter。我感谢您的帮助。您能否将我链接到一个好的在线资源?
猜你喜欢
  • 2016-08-15
  • 2017-04-26
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
相关资源
最近更新 更多