用python,tkin计算和显示结果

2024-10-01 13:41:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个Python初学者的帮助。我改编了一个来自R的程序,它对用户在Q and df字段中输入的两个变量(Q,df)进行一些计算。在

我的问题是:如何让程序在适当的字段(I2, 95%LL, 95%UL)中显示I2, I2_LL and I2_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

我正在努力解决的问题是:

^{2}$

感谢您对初学者的耐心:-)


Tags: lognormdfifmathsqrth2ul
2条回答

这应该能让你。。。在

而不是

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())

试试看这:在

^{pr2}$

我认为问题在于,您试图在填充条目之前获取条目的值。我建议你在你的按钮上连接一个函数来执行计算。结果是这样的:

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()

相关问题 更多 >