Python菜单更新

Python菜单更新,python,loops,menu,tkinter,Python,Loops,Menu,Tkinter,我正在用Python开发一个GUI,其中团队是从菜单中选择的。选择团队后,应执行一个命令,根据滑块中的值计算值PD。我有两个问题。1该值不会从滑块值更新。我正在使用.get函数,但它似乎不起作用。2当选择新团队时,滑块彼此堆叠。如何清除第一个集合,以便为每个团队创建一个新集合?这是我的密码:有什么想法吗 from Tkinter import * import tkMessageBox import Tkinter root = Tk() root.title("NBA Point Diff

我正在用Python开发一个GUI,其中团队是从菜单中选择的。选择团队后,应执行一个命令,根据滑块中的值计算值PD。我有两个问题。1该值不会从滑块值更新。我正在使用.get函数,但它似乎不起作用。2当选择新团队时,滑块彼此堆叠。如何清除第一个集合,以便为每个团队创建一个新集合?这是我的密码:有什么想法吗

from Tkinter import *
import tkMessageBox
import Tkinter


root = Tk()
root.title("NBA Point Differentials Predictor")
root.geometry("300x680")

mb =  Menubutton (root, text = "Select Team", relief = RAISED, pady = 8)
mb.grid()
mb.menu  =  Menu ( mb, tearoff = 0)
mb["menu"]  =  mb.menu


def sel():
    selection = "Point differential = " + str(PD)
    label.config(text = selection)

FGP = DoubleVar();TPP = DoubleVar(); FTP = DoubleVar(); ORB = DoubleVar(); TRB = DoubleVar();
AST = DoubleVar(); STL = DoubleVar(); BLK = DoubleVar(); TOV = DoubleVar(); PF = DoubleVar(); 

def ATL():

    index = 1

    if index == 1: 

        FGP_mean = 0.47; TPP_mean = 0.4; FTP_mean = 0.78; ORB_mean = 8.5;
        TRB_mean = 40.6; AST_mean = 25.8; STL_mean = 9.06; BLK_mean = 4.63;
        TOV_mean = 13.5; PF_mean = 17.7;

        slider_order = [5, 1, 9, 7, 6, 3, 4, 2, 8, 10]

        for x in slider_order:

            if x == 1:
                scale1 = Scale(root, from_=0, to=1, resolution = 0.01, variable = FGP, orient = HORIZONTAL, label = "Field goal percentage")
                scale1.set(FGP_mean)
                scale1.grid()
            elif x == 2:
                scale2 = Scale(root, from_=0, to=1, resolution = 0.01, variable = TPP, orient = HORIZONTAL, label = "3-point field goal percentage")
                scale2.set(TPP_mean)
                scale2.grid()
            elif x == 3:
                scale3 = Scale(root, from_=0, to=1, resolution = 0.01, variable = FTP, orient = HORIZONTAL, label = "Free throw percentage")
                scale3.set(FTP_mean)
                scale3.grid()
            elif x == 4:
                scale4 = Scale(root, from_=0, to=50, variable = ORB, orient = HORIZONTAL, label = "Offensive rebounds")
                scale4.set(ORB_mean)
                scale4.grid()
            elif x == 5:
                scale5 = Scale(root, from_=0, to=100, variable = TRB, orient = HORIZONTAL, label = "Total rebounds")
                scale5.set(TRB_mean)
                scale5.grid()
            elif x == 6:
                scale6 = Scale(root, from_=0, to=50, variable = AST, orient = HORIZONTAL, label = "Assists")
                scale6.set(AST_mean)
                scale6.grid()
            elif x == 7:
                scale7 = Scale(root, from_=0, to=30, variable = STL, orient = HORIZONTAL, label = "Steals")
                scale7.set(STL_mean)
                scale7.grid()
            elif x == 8:
                scale8 = Scale(root, from_=0, to=30, variable = BLK, orient = HORIZONTAL, label = "Blocks")
                scale8.set(BLK_mean)
                scale8.grid()
            elif x == 9:
                scale9 = Scale(root, from_=0, to=50, variable = TOV, orient = HORIZONTAL, label = "Turnovers")
                scale9.set(TOV_mean)
                scale9.grid()
            else:  
                scale10 = Scale(root, from_=0, to=50, variable = PF, orient = HORIZONTAL, label = "Personal fouls")
                scale10.set(PF_mean)
                scale10.grid()

        global PD

        PD = -123.9319 + 114.58945*FGP.get() + 12.48279*TPP.get() + 14.62438*FTP.get() - 0.41907*ORB.get() + 1.36049*TRB.get() + 0.46082*AST.get() + 1.17966*STL.get() + 0.18258*BLK.get() -1.16002*TOV.get() + 0.02497*PF.get()


def BOS():

    index = 2

    if index == 2:

        FGP_mean = 0.44; TPP_mean = 0.32; FTP_mean = 0.76; ORB_mean = 11.14;
        TRB_mean = 43.94; AST_mean = 24.22; STL_mean = 8.038; BLK_mean = 3.59;
        TOV_mean = 13.29; PF_mean = 21.24;

        slider_order = [1, 5, 9, 7, 4, 2, 10, 6, 3, 8]

        for x in slider_order:

            if x == 1:
                scale1 = Scale(root, from_=0, to=1, resolution = 0.01, variable = FGP, orient = HORIZONTAL, label = "Field goal percentage")
                scale1.set(FGP_mean)
                scale1.grid()
            elif x == 2:
                scale2 = Scale(root, from_=0, to=1, resolution = 0.01, variable = TPP, orient = HORIZONTAL, label = "3-point field goal percentage")
                scale2.set(TPP_mean)
                scale2.grid()
            elif x == 3:
                scale3 = Scale(root, from_=0, to=1, resolution = 0.01, variable = FTP, orient = HORIZONTAL, label = "Free throw percentage")
                scale3.set(FTP_mean)
                scale3.grid()
            elif x == 4:
                scale4 = Scale(root, from_=0, to=50, variable = ORB, orient = HORIZONTAL, label = "Offensive rebounds")
                scale4.set(ORB_mean)
                scale4.grid()
            elif x == 5:
                scale5 = Scale(root, from_=0, to=100, variable = TRB, orient = HORIZONTAL, label = "Total rebounds")
                scale5.set(TRB_mean)
                scale5.grid()
            elif x == 6:
                scale6 = Scale(root, from_=0, to=50, variable = AST, orient = HORIZONTAL, label = "Assists")
                scale6.set(AST_mean)
                scale6.grid()
            elif x == 7:
                scale7 = Scale(root, from_=0, to=30, variable = STL, orient = HORIZONTAL, label = "Steals")
                scale7.set(STL_mean)
                scale7.grid()
            elif x == 8:
                scale8 = Scale(root, from_=0, to=30, variable = BLK, orient = HORIZONTAL, label = "Blocks")
                scale8.set(BLK_mean)
                scale8.grid()
            elif x == 9:
                scale9 = Scale(root, from_=0, to=50, variable = TOV, orient = HORIZONTAL, label = "Turnovers")
                scale9.set(TOV_mean)
                scale9.grid()
            else:  
                scale10 = Scale(root, from_=0, to=50, variable = PF, orient = HORIZONTAL, label = "Personal fouls")
                scale10.set(PF_mean)
                scale10.grid()

        global PD

        PD = -95.6789 + 135.5622*FGP.get() + 13.2779*TPP.get() + 7.1998*FTP.get() - 0.5649*ORB.get() + 1.0487*TRB.get() - 0.2041*AST.get() + 1.4290*STL.get() - 0.2492*BLK.get() - 1.2071*TOV.get() - 0.1919*PF.get()



def BRK():

    FGP_mean = 0.45; TPP_mean = 0.33; FTP_mean = 0.75; ORB_mean = 10.24;
    TRB_mean = 42.28; AST_mean = 20.94; STL_mean = 7.0; BLK_mean = 4.18;
    TOV_mean = 13.45; PF_mean = 19.0;

    slider_order = [5, 1, 7, 9, 2, 4, 3, 6, 10, 8]

    for x in slider_order:

        if x == 1:
            scale1 = Scale(root, from_=0, to=1, resolution = 0.01, variable = FGP, orient = HORIZONTAL, label = "Field goal percentage")
            scale1.set(FGP_mean)
            scale1.grid()
        elif x == 2:
            scale2 = Scale(root, from_=0, to=1, resolution = 0.01, variable = TPP, orient = HORIZONTAL, label = "3-point field goal percentage")
            scale2.set(TPP_mean)
            scale2.grid()
        elif x == 3:
            scale3 = Scale(root, from_=0, to=1, resolution = 0.01, variable = FTP, orient = HORIZONTAL, label = "Free throw percentage")
            scale3.set(FTP_mean)
            scale3.grid()
        elif x == 4:
            scale4 = Scale(root, from_=0, to=50, variable = ORB, orient = HORIZONTAL, label = "Offensive rebounds")
            scale4.set(ORB_mean)
            scale4.grid()
        elif x == 5:
            scale5 = Scale(root, from_=0, to=100, variable = TRB, orient = HORIZONTAL, label = "Total rebounds")
            scale5.set(TRB_mean)
            scale5.grid()
        elif x == 6:
            scale6 = Scale(root, from_=0, to=50, variable = AST, orient = HORIZONTAL, label = "Assists")
            scale6.set(AST_mean)
            scale6.grid()
        elif x == 7:
            scale7 = Scale(root, from_=0, to=30, variable = STL, orient = HORIZONTAL, label = "Steals")
            scale7.set(STL_mean)
            scale7.grid()
        elif x == 8:
            scale8 = Scale(root, from_=0, to=30, variable = BLK, orient = HORIZONTAL, label = "Blocks")
            scale8.set(BLK_mean)
            scale8.grid()
        elif x == 9:
            scale9 = Scale(root, from_=0, to=50, variable = TOV, orient = HORIZONTAL, label = "Turnovers")
            scale9.set(TOV_mean)
            scale9.grid()
        else:  
            scale10 = Scale(root, from_=0, to=50, variable = PF, orient = HORIZONTAL, label = "Personal fouls")
            scale10.set(PF_mean)
            scale10.grid()

    global PD

    PD = -143.6673 + 139.9460*FGP.get() + 29.1870*TPP.get() + 20.6729*FTP.get() - 0.8086*ORB.get() + 1.4133*TRB.get() - 0.2628*AST.get() + 1.8273*STL.get() + 0.3091*BLK.get() - 0.8778*TOV.get() + 0.1976*PF.get()

mb.menu.add_command(label="Atlanta Hawks", command=ATL)
mb.menu.add_command(label="Boston Celtics", command=BOS)
mb.menu.add_command(label="Brooklyn Nets", command=BRK)



app = Frame(root)
app.grid

button = Button(root, text = "Get Point Differential", command=sel)
button.grid()

label = Label(root)
label.grid()

root.config(menu=mb)
root.mainloop()

您的问题是PD的计算是在ATL、BOS和BRK函数中完成的,只有在您从菜单中选择团队时才会调用这些函数

将计算从这些函数中删除,并将其放入自己的函数中。现在,您可以使用以下工具跟踪ORB、TRB等变量:

ORB.trace("w", calculate_points)

下面将修复您所遇到的错误,并清除严重的代码重复。没有更新数值的问题正如@TidB所说的:只有在选择新团队时才进行计算。滑块堆叠在一起的问题是因为您反复创建新滑块并将它们放置在同一位置,而不是使用gridrow=正确行的编号重新排列它们

注意:我在Python3中对此进行了测试。这个程序唯一的区别应该是在Python2中它是import Tkinter,大写字母T,而在Python3中它是import Tkinter,小写字母T


但是每个PD变量对于ATL、BOS等函数都是唯一的。在这种情况下,您可以有一个包含ATL、BOS或BRK的变量,该变量在您选择团队时会更新。在calculate_points或任何将被调用的函数中,您可以检查此变量并为所选团队选择特定的计算。无论如何,我强烈建议您将GUI重构为一个类;这有助于让那些丑陋的地球人摆脱困境。谢谢你的帮助!很好。如果这个答案解决了你的问题,请随意投票/接受。
from Tkinter import *

root = Tk()
root.title("NBA Point Differentials Predictor")
root.geometry("150x680")

mb =  Menubutton (root, text = "Select Team", relief = RAISED, pady = 8)
mb.grid()
mb.menu  =  Menu ( mb, tearoff = 0)
mb["menu"]  =  mb.menu

PD = []

def sel():
    selection = "Point differential = %0.3f" % (PD[0] + PD[1][0]*FGP.get() + PD[1][1]*TPP.get() + PD[1][2]*FTP.get() +
    PD[1][3]*ORB.get() + PD[1][4]*TRB.get() + PD[1][5]*AST.get() + PD[1][6]*STL.get() + PD[1][7]*BLK.get() +
    PD[1][8]*TOV.get() + PD[1][9]*PF.get())
    label.config(text = selection)

FGP, TPP, FTP, ORB, TRB, AST, STL, BLK, TOV, PF = (DoubleVar() for i in range(10))

all_scales = [
    Scale(root, from_=0, to=1, resolution = 0.01, variable = FGP, orient = HORIZONTAL, label = "Field goal percentage"),
    Scale(root, from_=0, to=1, resolution = 0.01, variable = TPP, orient = HORIZONTAL, label = "3-point field goal percentage"),
    Scale(root, from_=0, to=1, resolution = 0.01, variable = FTP, orient = HORIZONTAL, label = "Free throw percentage"),
    Scale(root, from_=0, to=50, variable = ORB, orient = HORIZONTAL, label = "Offensive rebounds"),
    Scale(root, from_=0, to=100, variable = TRB, orient = HORIZONTAL, label = "Total rebounds"),
    Scale(root, from_=0, to=50, variable = AST, orient = HORIZONTAL, label = "Assists"),
    Scale(root, from_=0, to=30, variable = STL, orient = HORIZONTAL, label = "Steals"),
    Scale(root, from_=0, to=30, variable = BLK, orient = HORIZONTAL, label = "Blocks"),
    Scale(root, from_=0, to=50, variable = TOV, orient = HORIZONTAL, label = "Turnovers"),
    Scale(root, from_=0, to=50, variable = PF, orient = HORIZONTAL, label = "Personal fouls")
    ]

def ATL(scales):
    slider_order = [5, 1, 9, 7, 6, 3, 4, 2, 8, 10]
    means = [0.47, 0.4, 0.78, 8.5, 40.6, 25.8, 9.06, 4.63, 13.5, 17.7]
    for order, scale, mean in zip(slider_order, scales, means):
            scale.set(mean)
            scale.grid(row=order+3)
    global PD
    PD = [-123.9319, [114.58945, 12.48279, 14.62438, -0.41907, 1.36049, 0.46082, 1.17966, 0.18258, -1.16002, 0.02497]]

def BOS(scales):
    slider_order = [1, 5, 9, 7, 4, 2, 10, 6, 3, 8]
    means = [0.44, 0.32, 0.76, 11.14, 43.94, 24.22, 8.038, 3.59, 13.29, 21.24]
    for order, scale, mean in zip(slider_order, scales, means):
            scale.set(mean)
            scale.grid(row=order+3)
    global PD
    PD = [-95.6789, [135.5622, 13.2779, 7.1998, -0.5649, 1.0487, -0.2041, 1.4290, -0.2492, -1.2071, -0.1919]]

def BRK(scales):
    slider_order = [5, 1, 7, 9, 2, 4, 3, 6, 10, 8]
    means = [0.45, 0.33, 0.75, 10.24, 42.28, 20.94, 7.0, 4.18, 13.45, 19.0]
    for order, scale, mean in zip(slider_order, scales, means):
            scale.set(mean)
            scale.grid(row=order+3)
    global PD
    PD = [-143.6673, [139.9460, 29.1870, 20.6729, -0.8086, 1.4133, -0.2628, 1.8273, 0.3091, -0.8778, 0.1976]]

mb.menu.add_command(label="Atlanta Hawks", command=lambda: ATL(all_scales))
mb.menu.add_command(label="Boston Celtics", command=lambda: BOS(all_scales))
mb.menu.add_command(label="Brooklyn Nets", command=lambda: BRK(all_scales))

app = Frame(root)
app.grid()

button = Button(app, text = "Get Point Differential", command=sel)
button.grid()

label = Label(app)
label.grid()

root.config(menu=mb)
root.mainloop()