0

This is the the whole code:

import tkinter as tk
from tkinter import ttk

def click():
    global totalvalues
    if combo_impact.get()and combo_likelihood.get() != '':
        totalvalues= int(combo_impact.get()) + int(combo_likelihood.get())

    total.configure(text="Risk score : " + str(totalvalues))



def main():
    

    impactDict = {'Catstrophic': 5, 'Severe': 4, 'Major': 3, 'Moderate': 2, 'Minor':1}
    likelihoodDict = {'Almost Certain': 5, 'likely': 4, 'probable': 3, 'unlikely': 2, 'rare':1}

    root = tk.Tk()
    root.title("TEST Form")
    root.configure(bg='Dodgerblue4')

    tk.Label(root, text='legal Risk Impact', bd=3, bg='Dodgerblue4', fg='white').grid(row=0, column=0)
    var_impact = tk.StringVar()
    combo_impact = ttk.Combobox(root, values=list(impactDict.keys()), justify="center", textvariable=var_impact)
    combo_impact.bind('<<ComboboxSelected>>', lambda event: impact_level.config(text=impactDict[var_impact.get()]))
    combo_impact.grid(row=0, column=1)
    combo_impact.current(0)

    impact_level = tk.Label(root, text="", bg='Dodgerblue4', fg='white')
    impact_level.grid(row=0, column=2, padx=10)

    tk.Label(root, text='Legal risk Likelihood', bd=3, bg='Dodgerblue4', fg='white').grid(row=1, column=0)
    var_likelihood = tk.StringVar()
    combo_likelihood = ttk.Combobox(root, values=list(likelihoodDict.keys()), justify="center", textvariable=var_likelihood)
    combo_likelihood.bind('<<ComboboxSelected>>', lambda event: likelihood_level.config(text=likelihoodDict[var_likelihood.get()]))
    combo_likelihood.grid(row=1, column=1)
    combo_likelihood.current(0)

    likelihood_level = tk.Label(root, text="Not Selected", bg='Dodgerblue4', fg='white')
    likelihood_level.grid(row=1, column=2, padx=10)

    totalvalues = 'None'
    total = tk.Label(root,text = "", bg='Dodgerblue4', fg='white' )
    total.grid(row=2, column = 2, padx=20, pady=10)

    calculate=tk.Button(root,text='Rate', command=click).grid(column=3,row=3)

    root.mainloop()

if __name__ == '__main__':
    main()
2
  • You should be using global combo_impact, combo_likelihood, impactDict, likelihoodDict, total inside main(), then totalvalues= int(impactDict[combo_impact.get()]) + int(likelihoodDict[combo_likelihood.get()]), you need to index the dictionary with the fetched values, like so. Commented Sep 21, 2021 at 9:46
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Sep 28, 2021 at 8:31

1 Answer 1

1

This is what I want to code> i have resolved the issue in non-pytonic way using too long codes. Although I have tried to bring my idea into some glitter of hope. I am not satisfied with my code. Because it is not unprofessional. For the purpose of education, I am sharing the resolved non-pythonic code from my trial and error. It can be a good education tool for some instructors:

    import tkinter as tk
    from tkinter import ttk

def click(*args):

global totalvalues
global combo_impact
global combo_likelihood
global impactDict
global likelihoodDict
global total
global risk_level
global x
global y
global negligiblelabel2
global negligiblelabel3
global negligiblelabel1
global lowlabel1
global lowlabel2
global lowlabel3
global lowlabel4
global lowlabel5
global mediumlabel1
global mediumlabel2
global mediumlabel3
global mediumlabel4
global mediumlabel5
global mediumlabel6
global mediumlabel7
global highlabel1
global highlabel2
global highlabel3
global highlabel4
global extremelabel1
global extremelabel2
global extremelabel3
global extremelabel4
global extremelabel5
global extremelabel6


if combo_impact.get()and combo_likelihood.get() != '':
    totalvalues= int(impactDict[combo_impact.get()]) * int(likelihoodDict[combo_likelihood.get()])
    
total.configure(text="Risk score : " + str(totalvalues))

x = int(impactDict[combo_impact.get()])
y = int(likelihoodDict[combo_likelihood.get()])

if x == 1 and y==1:
    negligiblelabel2.configure(text="Negligible risk")

elif x == 1 and y==2:
    negligiblelabel3.configure(text="Negligible risk")
elif x == 2 and y==1:
    negligiblelabel1.configure(text="Negligible risk")
elif x == 1 and y==3:
    lowlabel4.configure(text="Low risk")
elif x == 3 and y==1:
    lowlabel2.configure(text="Low risk")
elif x == 1 and y==4:
    lowlabel5.configure(text="Low risk")
elif x == 4 and y==1:
    lowlabel1.configure(text="Low risk")
elif x == 2 and y==2:
    lowlabel3.configure(text="Low risk")
elif x == 2 and y==3:
    mediumlabel5.configure(text="Medium risk")
elif x == 3 and y==2:
    mediumlabel3.configure(text="Medium risk")
elif x == 2 and y==4:
    mediumlabel6.configure(text="Medium risk")
elif x == 4 and y==2:
    mediumlabel2.configure(text="Medium risk")
elif x == 3 and y==3:
    mediumlabel4.configure(text="Medium risk")
elif x == 1 and y==5:
    mediumlabel7.configure(text="Medium risk")
elif x == 5 and y==1:
    mediumlabel1.configure(text="Medium risk")
elif x == 2 and y==5:
    highlabel4.configure(text = "High risk") 
elif x == 5 and y==2:
    highlabel1.configure(text = "High risk")
elif x == 3 and y==4:
    highlabel3.configure(text = "High risk")
elif x == 4 and y==3:
    highlabel2.configure(text = "High risk")
elif x == 3 and y==5:
    extremelabel6.configure(text= "Extreme risk")
elif x == 5 and y==3:
    extremelabel1.configure(text= "Extreme risk")
elif x == 4 and y ==4:
    extremelabel3.configure(text= "Extreme risk")
elif x == 4 and y==5:
    extremelabel5.configure(text= "Extreme risk")
elif x == 5 and y==4:
    extremelabel2.configure(text= "Extreme risk")
elif x == 5 and y==5:
    extremelabel4.configure(text= "Extreme risk")
else:
    print('wrong input')

def main(): global combo_impact global combo_likelihood global impactDict global likelihoodDict global total global risk_level global negligiblelabel2 global negligiblelabel1 global negligiblelabel3 global lowlabel1 global lowlabel2 global lowlabel3 global lowlabel4 global lowlabel5 global mediumlabel1 global mediumlabel2 global mediumlabel3 global mediumlabel4 global mediumlabel5 global mediumlabel6 global mediumlabel7 global highlabel1 global highlabel2 global highlabel3 global highlabel4 global extremelabel1 global extremelabel2 global extremelabel3 global extremelabel4 global extremelabel5 global extremelabel6

impactDict = {'Catstrophic': 5, 'Severe': 4, 'Major': 3, 'Moderate': 2, 'Minor':1}
likelihoodDict = {'Almost Certain': 5, 'likely': 4, 'possible': 3, 'unlikely': 2, 'rare':1}

root = tk.Tk()
root.title("TEST Form")
root.configure(bg='Dodgerblue4')

tk.Label(root, text='legal Risk Impact', bd=3, bg='Dodgerblue4', fg='white').grid(row=0, column=0)
var_impact = tk.StringVar()
combo_impact = ttk.Combobox(root, values=list(impactDict.keys()), justify="center", textvariable=var_impact)
combo_impact.bind('<<ComboboxSelected>>', lambda event: impact_level.config(text=impactDict[var_impact.get()]))
combo_impact.grid(row=0, column=1)
combo_impact.current(0)

impact_level = tk.Label(root, text="", bg='Dodgerblue4', fg='white')
impact_level.grid(row=0, column=2, padx=10)

tk.Label(root, text='Legal risk Likelihood', bd=3, bg='Dodgerblue4', fg='white').grid(row=1, column=0)
var_likelihood = tk.StringVar()
combo_likelihood = ttk.Combobox(root, values=list(likelihoodDict.keys()), justify="center", textvariable=var_likelihood)
combo_likelihood.bind('<<ComboboxSelected>>', lambda event: likelihood_level.config(text=likelihoodDict[var_likelihood.get()]))
combo_likelihood.grid(row=1, column=1)
combo_likelihood.current(0)

likelihood_level = tk.Label(root, text="", bg='Dodgerblue4', fg='white')
likelihood_level.grid(row=1, column=2, padx=10)

totalvalues = 'None'
total = tk.Label(root,text = "", bg='Dodgerblue4', fg='white' )
total.grid(row=2, column = 1, padx=20, pady=10)

calculate=tk.Button(root,width = 10, text='Rate', command=click).grid(column=0,row=2, padx=1, pady=1)

matrixframe = tk.Frame(root, highlightbackground='#292A2E', bd=1)
matrixframe.grid(row=3, column = 3, padx=20, pady=10)
impactlabel = tk.Label(matrixframe, height= 5, width = 10, text = "Impact", bg='#384455', fg='#384455')
impactlabel.grid(row=0, column = 0)
impactlabel1 = tk.Label(matrixframe, height= 5, width = 10, text = "Impact", bg='#384455', fg='#384455')
impactlabel1.grid(row=1, column = 0)
impactlabel2 = tk.Label(matrixframe, height= 5, width = 10, text = "Impact", bg='#384455', fg='white')
impactlabel2.grid(row=2, column = 0)
impactlabel3 = tk.Label(matrixframe, height= 5, width = 10, text = "Impact", bg='#384455', fg='#384455')
impactlabel3.grid(row=3, column = 0)
impactlabel4 = tk.Label(matrixframe, height= 5, width = 10, text = "Impact", bg='#384455', fg='#384455')
impactlabel4.grid(row=4, column = 0)
catstrophiclabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 12, text = "Catstrophic", bg='#FFFFFF', fg='black')
catstrophiclabel.grid(row=0, column = 1)
severelabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 12, text = "Severe", bg='#FFFFFF', fg='black')
severelabel.grid(row=1, column = 1)
majorlabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 12, text = "Major", bg='#FFFFFF', fg='black')
majorlabel.grid(row=2, column = 1)
moderatelabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 12, text = "Moderate", bg='#FFFFFF', fg='black')
moderatelabel.grid(row=3, column = 1)
minorlabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 12, text = "Minor", bg='#FFFFFF', fg='black')
minorlabel.grid(row=4, column = 1)
mediumlabel1 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFFF00', fg='black')
mediumlabel1.grid(row=0, column = 2)
lowlabel1 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#B5ED46', fg='black')
lowlabel1.grid(row=1, column = 2)
lowlabel2 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#B5ED46', fg='black')
lowlabel2.grid(row=2, column = 2)
negligiblelabel1 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#00B050', fg='black')
negligiblelabel1.grid(row=3, column = 2)
negligiblelabel2 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#00B050', fg='black')
negligiblelabel2.grid(row=4, column = 2)
highlabel1 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFC000', fg='black')
highlabel1.grid(row=0, column = 3)
mediumlabel2 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFFF00', fg='black')
mediumlabel2.grid(row=1, column = 3)
mediumlabel3 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFFF00', fg='black')
mediumlabel3.grid(row=2, column = 3)
lowlabel3 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#B5ED46', fg='black')
lowlabel3.grid(row=3, column = 3)
negligiblelabel3 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#00B050', fg='black')
negligiblelabel3.grid(row=4, column = 3)
extremelabel1 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FF0000', fg='black')
extremelabel1.grid(row=0, column = 4)
highlabel2 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFC000', fg='black')
highlabel2.grid(row=1, column = 4)
mediumlabel4 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFFF00', fg='black')
mediumlabel4.grid(row=2, column = 4)
mediumlabel5 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFFF00', fg='black')
mediumlabel5.grid(row=3, column = 4)
lowlabel4 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#B5ED46', fg='black')
lowlabel4.grid(row=4, column = 4)
extremelabel2 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FF0000', fg='black')
extremelabel2.grid(row=0, column = 5)
extremelabel3 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FF0000', fg='black')
extremelabel3.grid(row=1, column = 5)
highlabel3 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFC000', fg='black')
highlabel3.grid(row=2, column = 5)
mediumlabel6 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFFF00', fg='black')
mediumlabel6.grid(row=3, column = 5)
lowlabel5 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#B5ED46', fg='black')
lowlabel5.grid(row=4, column = 5)
extremelabel4 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FF0000', fg='black')
extremelabel4.grid(row=0, column = 6)
extremelabel5 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FF0000', fg='black')
extremelabel5.grid(row=1, column = 6)
extremelabel6 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FF0000', fg='black')
extremelabel6.grid(row=2, column = 6)
highlabel4 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFC000', fg='black')
highlabel4.grid(row=3, column = 6)
mediumlabel7 = tk.Label(matrixframe, height= 5, width = 15, relief = 'groove', text = "", bg='#FFFF00', fg='black')
mediumlabel7.grid(row=4, column = 6)
heatmaplabel = tk.Label(matrixframe, height= 5, width = 10, text = "Heat Map", bg='#FFFFFF', fg='#384455')
heatmaplabel.grid(row=5, column = 0)
heatmaplabel1 = tk.Label(matrixframe, height= 5, width = 12, text = "Heat Map", bg='#FFFFFF', fg='#FFFFFF')
heatmaplabel1.grid(row=5, column = 1)
rarelabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 15, text = "Rare", bg='#FFFFFF', fg='black')
rarelabel.grid(row=5, column = 2)
unlikelylabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 15, text = "Unlikely", bg='#FFFFFF', fg='black')
unlikelylabel.grid(row=5, column = 3)
possiblelabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 15, text = "Possible", bg='#FFFFFF', fg='black')
possiblelabel.grid(row=5, column = 4)
likelylabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 15, text = "Likely", bg='#FFFFFF', fg='black')
likelylabel.grid(row=5, column = 5)
almostcertainlabel = tk.Label(matrixframe, height= 5, relief = 'groove', width = 15, text = "Almost certain", bg='#FFFFFF', fg='black')
almostcertainlabel.grid(row=5, column = 6)
heatmaplabel2 = tk.Label(matrixframe, height= 5, width = 10, text = "Heat Map", bg='#FFFFFF', fg='#FFFFFF')
heatmaplabel2.grid(row=6, column = 0)
heatmaplabel3 = tk.Label(matrixframe, height= 5, width = 12, text = "Heat Map", bg='#FFFFFF', fg='#FFFFFF')
heatmaplabel3.grid(row=6, column = 1)
likelihoodlabel = tk.Label(matrixframe, height= 5, width = 15, text = "", bg='#384455', fg='#384455')
likelihoodlabel.grid(row=6, column = 2)
likelihoodlabel = tk.Label(matrixframe, height= 5, width = 15, text = "", bg='#384455', fg='#384455')
likelihoodlabel.grid(row=6, column = 3)
likelihoodlabel = tk.Label(matrixframe, height= 5, width = 15, text = "Likelihood", bg='#384455', fg='#FFFFFF')
likelihoodlabel.grid(row=6, column = 4)
likelihoodlabel = tk.Label(matrixframe, height= 5, width = 15, text = "", bg='#384455', fg='#384455')
likelihoodlabel.grid(row=6, column = 5)
likelihoodlabel = tk.Label(matrixframe, height= 5, width = 15, text = "", bg='#384455', fg='#384455')
likelihoodlabel.grid(row=6, column = 6)


root.mainloop()

if name == 'main': main()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.