如何通过在Tkinter中按下按钮来将Var显示为标签

时间:2018-06-23 08:22:10

标签: python tkinter

我是Tkinter的新用户,但我想使用 .get 方法从2个输入小部件中获取两个条目,并获取均值和标准差,这些均值和标准差也显示在其他2个标签中。问题是,我没有显示均值和标准差。单击SD区域按钮时,它也无法显示返回的参数。请帮忙。这是代码:

from tkinter import *
from tkinter import Tk, StringVar, ttk
import random
import datetime


#====================Root of the app======================================
root = Tk()
root.geometry("500x300+0+0")
root.title("SD zone finder")
root.resizable(width = False, height = False)
#Top frame that has the widgets
Topframe = Frame(root, width = 500, height = 265, bd = 5, relief = 'raise')
Topframe.pack(side = TOP)

#Bottom frame just to display my name
Bottomframe = Frame(root, width = 500, height = 50, bd = 5, relief = 'raise')
Bottomframe.pack(side = BOTTOM)

labelbottom = Label(Bottomframe, font = ('garamond', 12, 'italic'), width = 60, text = "Muhammad", justify = 'center')
labelbottom.grid(row = 0, column = 0)

#========================Input Variables==============================
#These the the input and out put variables for the labels and entry widgets
var1 = DoubleVar()
var2 = DoubleVar()
var3 = DoubleVar()
var4 = DoubleVar()
var5 = DoubleVar()
var6 = StringVar()

var1.set("0.0")
var2.set("0.0")
var3.set('mean')
var4.set('std_dev')
var5.set("0.0")
var6.set('y')
#These are the formulae for mean and standard deviation for floats entered in the entry widgets
mean = float((var1.get())+(var2.get())/2)
std_dev = float((((var1.get())-mean)**2+((var2.get())-mean)**2)/(2-1)**(1/2))
y = float(var5.get())
#=====================Button Press Functions=================================
#This is the function that is to be called when user clicks the Mean and SD button
def stats():
    var3.set(mean)
    var4.set(std_dev)

#This is the function to call when the user clicks the SD Zone button
def sdzone():
    if y == mean:
        var6.set("Along mean")
    elif mean > y < ((1/2)*std_dev):
        var6.set("+0.5SD")
    elif (mean+((1/2)*std_dev)) > y < (mean+std_dev):
        var6.set("+1SD")
    elif (mean+std_dev) > y < (mean+1.5*std_dev):
        var6.set("+1.5SD")
    elif (mean+1.5*std_dev) > y < (mean+2*std_dev):
        var6.set("+2SD")
    elif (mean+2*std_dev) > y < (mean+2.5*std_dev):
        var6.set("+2.5SD")
    elif (mean+2.5*std_dev) > y < (mean+3*std_dev):
        var6.set("+3SD")
    elif mean > y > (mean-(1/2)*std_dev):
        var6.set("-0.5SD")
    elif (mean-(1/2)*std_dev) > y > (mean-std_dev):
        var6.set("-1SD")
    elif (mean-std_dev) > y > (mean-1.5*std_dev):
        var6.set("-1.5SD")
    elif (mean-1.5*std_dev) > y > (mean-2*std_dev):
        var6.set("-2SD")
    elif (mean-2*std_dev) > y > (mean-2.5*std_dev):
        var6.set("-2.5SD")
    elif (mean-2.5*std_dev) > y > (mean-3*std_dev):
        var6.set("-3SD")
    else:
        var6.set("Out of 3SD")

#=======================Stats Labels========================================

#These are the widget details for labels and entry fields
Run1Label = Label(Topframe, font = ('garamond', 14), height = 2, text = "Enter first run here:",  justify = 'left')
Run1Label.grid(row = 0, column = 0, sticky = W)

Run2Label = Label(Topframe, font = ('garamond', 14), height = 2, text = "Enter second run here:",  justify = 'left')
Run2Label.grid(row = 1, column = 0, sticky = W)

MeanLabel = Label(Topframe, font = ('garamond', 14), height = 2, text = "The mean is:",  justify = 'left')
MeanLabel.grid(row = 2, column = 0, sticky = W)

SDLabel = Label(Topframe, font = ('garamond', 14), height = 2, text = "The SD is:",  justify = 'left')
SDLabel.grid(row = 2, column = 3, sticky = W)

#==========================SD Zone labels===================================
DailyrunLabel = Label(Topframe, font = ('garamond', 14), height = 2, text = "Enter QC run here:",  justify = 'left')
DailyrunLabel.grid(row = 3, column = 0, sticky = W)

SDzoneLabel = Label(Topframe, font = ('garamond', 14), height = 2, text = "The SD zone is:",  justify = 'left')
SDzoneLabel.grid(row = 4, column = 0, sticky = W)

#=======================Stats Entries and Displays==========================
Run1entry = Entry(Topframe, font = ('garamond', 14), width = 10, bd = 5, textvariable = var1,  relief = 'sunken')
Run1entry.grid(row = 0, column = 1, sticky = W)

Run2entry = Entry(Topframe, font = ('garamond', 14), width = 10, bd = 5, textvariable = var2,  relief = 'sunken')
Run2entry.grid(row = 1, column = 1, sticky = W)

Dailyrunentry = Entry(Topframe, font = ('garamond', 14), width = 10, bd = 5, textvariable = var5,  relief = 'sunken')
Dailyrunentry.grid(row = 3, column = 1, sticky = W)

Mean2Label = Label(Topframe, font = ('garamond', 14), width = 7, bd = 5, textvariable = var3,  relief = 'sunken', anchor = 'w')
Mean2Label.grid(row = 2, column = 1, sticky = W)

SD2Label = Label(Topframe, font = ('garamond', 14), width = 7, bd = 5, textvariable = var4,  relief = 'sunken', anchor = 'w')
SD2Label.grid(row = 2, column = 4, sticky = W)

SDzoneLabel = Label(Topframe, font = ('garamond', 14), width = 7, bd = 5, textvariable = var6,  relief = 'sunken', anchor = 'w')
SDzoneLabel.grid(row = 4, column = 1, sticky = W)

#=====================Buttons===============================================
#These are the two buttons of the app
StatsButton = Button(Topframe, font = ('garamond', 14), width = 10, height = 2, bd = 5, text = "Mean and SD", command = stats())
StatsButton.grid(row = 0, column = 4, rowspan = 2)

SDButton = Button(Topframe, font = ('garamond', 14), width = 10, height = 2, bd = 5, text = "Find SD Zone", command = sdzone())
SDButton.grid(row = 3, column = 4, rowspan = 2)




root.mainloop()

1 个答案:

答案 0 :(得分:0)

您的按钮无法正常工作。

当您从按钮调用函数时,不应该

command = foo()

应该是

command = foo

如果要通过按钮将参数发送到函数中,可以使用lamda

command=lambda: foo(x)