-1

I am sort of a beginner.

I want to say that if the number in label9 >= 60 to display a messagebox saying one thing and if its <60 i want it to display a different messagebox saying a different thing. Do I use the if/else/elif statement or what?

def create_window():
    new = Toplevel()
    new.title('Lockdown Tracker')
    new.geometry('950x500')
    new.configure(bg="gray")
    
    label2 = Label(new, text="Please answer the questions below.", font=('Helvetica 30 bold'), relief= RIDGE)
    label2.place(rely=0)
    
    label3 = Label(new, text = "Q1. Rate the severity of the economic situation in your country. (1-100) *", font = ("Arial", 25))
    label3.place(rely=0.1)
    text3=Entry(new, width = "60")
    text3.place(rely=0.15)
    
    label4= Label(new, text="Q2. Rate the speed of vaccination in your country. (1-100) *", font = ("Arial", 25))
    label4.place(rely=0.21)  
    text4=Entry(new, width = "60")
    text4.place(rely=0.26)
    
    label5= Label(new, text="Q3. Rate how much the regulations are enforced and safety measures are taken. (1-100) *", font = ("Arial", 25))
    label5.place(rely=0.31)  
    text5=Entry(new, width = "60")
    text5.place(rely=0.36)
    
    label6= Label(new, text="Q4. What is the percentage of vaccinated people?(%) *", font = ("Arial", 25))
    label6.place(rely=0.41)  
    text6=Entry(new, width = "60")
    text6.place(rely=0.46)
    
    label7= Label(new, text="Q5. What is the ratio of positivity in the last week?(%) *", font = ("Arial", 25))
    label7.place(rely=0.51) 
    text7=Entry(new, width = "60")
    text7.place(rely=0.56)
    
    label8= Label(new, text="Q6. What is the percentage of active cases?(%) *", font = ("Arial", 25))
    label8.place(rely=0.61)
    text8=Entry(new, width = "60")
    text8.place(rely=0.66)
    
    
    
    def cmd3():
        label9 = Label(new, text = int(text3.get())/6 + int(text4.get())/6 + int(text5.get())/6 + int(text6.get())/6 + int(text7.get())/6 + int(text8.get())/6)
        label9.place(rely = 0.85)
        
        
window.mainloop() ```
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

Yes, you would use an if/else statement. In order to check the value of the text in label9, I've added another variable label9Text. This can then be used in an if/else statement to compare it. Depending on the value, a different messagebox is shown. (If you are unsure about how to import messagebox, add from tkinter import messagebox at the top of the program. You can change showinfo to showerror or showwarning depending on which icon you want.)

def cmd3():
    label9Text = int(text3.get())/6 + int(text4.get())/6 + int(text5.get())/6 + int(text6.get())/6 + int(text7.get())/6 + int(text8.get())/6
    label9 = Label(new, text = label9Text)
    label9.place(rely = 0.85)
    if label9Text >= 60:
        messagebox.showinfo("Title", "Text for >=60")
    else:
        messagebox.showinfo("Title", "Text for <60")
Henry
  • 3,472
  • 2
  • 12
  • 36
0

I think this should work:

if label9["text"] >= 60:
    messagebox.showinfo(window, "something")
else:
    messagebox.showinfo(window, "something else")
Roni
  • 597
  • 5
  • 21