我是python的初学者,我需要tkinter的帮助。我已经改编了一个来自R的程序,它对用户在Q and df
字段中输入的两个变量(Q,df)进行了一些计算。
我的问题是:如何让程序在相应字段I2, I2_LL and I2_UL
中显示(I2, 95%LL, 95%UL)
结果?
我的原始代码在没有图形界面的情况下完美运行是:
import math
from scipy.stats import norm
while True:
while True:
try:
Q = float(input("Introduceti valoarea Q: "))
except ValueError:
print("Introdu un numar!")
continue
if Q < 0:
print("Q trebuie sa aiba o valoare pozitiva")
continue
else:
break
while True:
try:
df = float(input("Introduceti valoarea df: "))
except ValueError:
print("Introdu un numar!")
continue
if df < 2:
print("df trebuie sa aiba o valoare mai mare sau egala cu 2")
continue
else:
break
K = df + 1
level = 95
levelci = level * 0.005 + 0.50
clevelci = 1 - levelci
if Q >= K:
SElnH = 0.5 * ((math.log(Q) - math.log(df)) / (math.sqrt(2 * Q) - math.sqrt(2 * K - 3)))
else:
SElnH = math.sqrt((1 / (2 * (K - 2)) * (1 - 1 / (3 * (K - 2) ** 2))))
H = math.sqrt(Q / df)
H2 = (Q / df)
H_LL = math.exp(math.log(math.sqrt(H2)) - norm.ppf(levelci) * SElnH)
H_UL = math.exp(math.log(math.sqrt(H2)) + norm.ppf(levelci) * SElnH)
I2 = (100 * (Q - df)) / Q
I22 = (H2 - 1) / H2
varI2 = 4 * SElnH ** 2 / math.exp(4 * math.log(math.sqrt(H2)))
I2_LL = (I22 - norm.ppf(levelci) * math.sqrt(varI2)) * 100
I2_UL = (I22 + norm.ppf(levelci) * math.sqrt(varI2)) * 100
print("--------------------------------------------------------------------------")
print("Statistic" + " " + " |" + "Estimate" + " " + " [95% Confidence Interval]")
print("--------------------------------------------------------------------------")
print(" H" + " ------->" + "|" + str(H) + " [" + str(H_LL) + " to " + str(H_UL) + "]")
print("I^2" + " ------->" + "|" + str(I2) + " [" + str(I2_LL) + " to " + str(I2_UL) + "]")
print("--------------------------------------------------------------------------")
while True:
answer = str(input("Introduceti alte valori? (da/nu): "))
if answer in ("da, nu"):
break
print("Raspuns invalid.")
if answer == "da":
continue
else:
print("Bye bye")
break
我正在努力解决的代码是:
import tkinter as tk
import math
from scipy.stats import norm
window = tk.Tk()
window.title("Heterogi")
window.geometry("400x400")
#Data input fields
label_q = tk.Label(window, text="Q: ")
label_q.grid(row=0, sticky="E")
label_df = tk.Label(window, text="df: ")
label_df.grid(row=1, sticky="E")
entry_q = tk.Entry(window)
entry_q.grid(row=0, column=1)
entry_df = tk.Entry(window)
entry_df.grid(row=1, column=1)
#Read from data input fields
Q = float(entry_q.get())
df = float(entry_df.get())
#Formulas
K = df + 1
level = 95
levelci = level * 0.005 + 0.50
if Q >= K:
SElnH = 0.5 * ((math.log(Q) - math.log(df)) / (math.sqrt(2 * Q) - math.sqrt(2 * K - 3)))
else:
SElnH = math.sqrt((1 / (2 * (K - 2)) * (1 - 1 / (3 * (K - 2) ** 2))))
H2 = (Q / df)
I2 = (100 * (Q - df)) / Q
I22 = (H2 - 1) / H2
varI2 = 4 * SElnH ** 2 / math.exp(4 * math.log(math.sqrt(H2)))
I2_LL = (I22 - norm.ppf(levelci) * math.sqrt(varI2)) * 100
I2_UL = (I22 + norm.ppf(levelci) * math.sqrt(varI2)) * 100
print(I2)
#Compute button
button = tk.Button(text="Compute", bg="red")
button.grid(row=2, column=1)
#Results fields
label_I2 = tk.Label(window, text="I2: ")
label_I2.grid(row=4, sticky="E")
label_95LL = tk.Label(window, text="95% LL: ")
label_95LL.grid(row=4, column=2)
label_95UL = tk.Label(window, text="95% UL: ")
label_95UL.grid(row=5, column=2)
entry_I2 = tk.Text(window, width=15, height=1, bg="light grey")
entry_I2.grid(row=4, column=1)
entry95LL = tk.Text(window, width=15, height=1, bg="light grey")
entry95LL.grid(row=4, column=3)
entry95UL = tk.Text(window, width=15, height=1, bg="light grey")
entry95UL.grid(row=5, column=3)
window.mainloop()
感谢您对初学者的耐心: - )
答案 0 :(得分:0)
这应该让你去......
而不是
entry_q = tk.Entry(window)
entry_q.grid(row=0, column=1)
entry_df = tk.Entry(window)
entry_df.grid(row=1, column=1)
#Read from data input fields
Q = float(entry_q.get())
df = float(entry_df.get())
试试这个: -
entry_q = tk.Text(window)
entry_q.grid(row=0, column=1)
entry_df = tk.Text(window)
entry_df.grid(row=1, column=1)
#Read from data input fields
Q = float(entry_q.get("1.0","end-1c"))
df = float(entry_df.get("1.0","end-1c"))
答案 1 :(得分:0)
我认为问题是你在填写之前尝试获取条目的值。我建议你将一个函数连接到你的按钮来执行计算。结果是这样的:
import tkinter as tk
import math
from scipy.stats import norm
def compute():
#Read from data input fields
Q = float(entry_q.get())
df = float(entry_df.get())
#Formulas
K = df + 1
level = 95
levelci = level * 0.005 + 0.50
if Q >= K:
SElnH = 0.5 * ((math.log(Q) - math.log(df)) / (math.sqrt(2 * Q) - math.sqrt(2 * K - 3)))
else:
SElnH = math.sqrt((1 / (2 * (K - 2)) * (1 - 1 / (3 * (K - 2) ** 2))))
H2 = (Q / df)
I2 = (100 * (Q - df)) / Q
I22 = (H2 - 1) / H2
varI2 = 4 * SElnH ** 2 / math.exp(4 * math.log(math.sqrt(H2)))
I2_LL = (I22 - norm.ppf(levelci) * math.sqrt(varI2)) * 100
I2_UL = (I22 + norm.ppf(levelci) * math.sqrt(varI2)) * 100
entry_I2.configure(text=str(I2))
entry95LL.configure(text=str(I2_LL))
entry95UL.configure(text=str(I2_UL))
window = tk.Tk()
window.title("Heterogi")
window.geometry("400x400")
#Data input fields
label_q = tk.Label(window, text="Q: ")
label_q.grid(row=0, sticky="E")
label_df = tk.Label(window, text="df: ")
label_df.grid(row=1, sticky="E")
entry_q = tk.Entry(window)
entry_q.grid(row=0, column=1)
entry_df = tk.Entry(window)
entry_df.grid(row=1, column=1)
#Compute button
button = tk.Button(text="Compute", bg="red", command=compute)
button.grid(row=2, column=1)
#Results fields
label_I2 = tk.Label(window, text="I2: ")
label_I2.grid(row=4, sticky="E")
label_95LL = tk.Label(window, text="95% LL: ")
label_95LL.grid(row=4, column=2)
label_95UL = tk.Label(window, text="95% UL: ")
label_95UL.grid(row=5, column=2)
entry_I2 = tk.Label(window, width=15, height=1, bg="light grey")
entry_I2.grid(row=4, column=1)
entry95LL = tk.Label(window, width=15, height=1, bg="light grey")
entry95LL.grid(row=4, column=3)
entry95UL = tk.Label(window, width=15, height=1, bg="light grey")
entry95UL.grid(row=5, column=3)
window.mainloop()