简体   繁体   English

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

[英]How do I get to display a Var into a label by pressing a button in Tkinter

I am a new Tkinter user, but I want to use the .get method to take two entries from 2 entry widgets and get their mean and standard deviation which are also displayed into 2 other labels. 我是Tkinter的新用户,但我想使用.get方法从2个条目小部件中提取两个条目,并获取它们的均值和标准差,这些均值和标准差也显示在其他2个标签中。 The problem is that, I have failed to display the mean and standard deviation. 问题是,我没有显示均值和标准差。 It has also failed to display the parameter returned, when the SD Zone button is clicked. 单击SD区域按钮时,它也无法显示返回的参数。 Some help please. 请一些帮助。 Here is the code: 这是代码:

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

Your buttons aren't working properly. 您的按钮无法正常工作。

When you're calling a function from a button it shouldn't be 当您从按钮调用函数时,不应

command = foo()

Instead it should be 相反,它应该是

command = foo

If you want to send parameters into a function from a button you can use lamda 如果要通过按钮将参数发送到函数中,可以使用lamda

command=lambda: foo(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM