Home > Blockchain >  How to connect combobox option with if statement in tkinter Python?
How to connect combobox option with if statement in tkinter Python?

Time:12-19

i make a GUI with tkinter i have a code like this :

b = StringVar()
nb =["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"]
b_bulan = ttk.Combobox(root, textvariable=b, values=nb)
b_bulan.bind("<<ComboboxSelected>>")
b_bulan.pack()
p_bulan= "<<ComboboxSelected>>"

def buka():
    top = Toplevel()
    top.title("Waktu Sholat")
    head = Label(top, text="Bulan", font=("Arial",25,"bold"), justify=CENTER)
    head.grid(row=0,column=0,columnspan=7)
    if p_bulan == "Januari":
        bt1 = Button(top, text="1", padx=14, pady=10, font=("Arial",12,"bold"))
        bt1.grid(row=1,column=0)
        bt2 = Button(top, text="2", padx=14, pady=10, font=("Arial",12,"bold"))
        bt2.grid(row=1,column=1)
        bt3 = Button(top, text="3", padx=14, pady=10, font=("Arial",12,"bold"))
        bt3.grid(row=1,column=2)
        bt4 = Button(top, text="4", padx=14, pady=10, font=("Arial",12,"bold"))
        bt4.grid(row=1,column=3)
        bt5 = Button(top, text="5", padx=14, pady=10, font=("Arial",12,"bold"))
        bt5.grid(row=1,column=4)
        bt6 = Button(top, text="6", padx=14, pady=10, font=("Arial",12,"bold"))
        bt6.grid(row=1,column=5)
        bt7 = Button(top, text="7", padx=14, pady=10, font=("Arial",12,"bold"))
        bt7.grid(row=1,column=6)
        bt8 = Button(top, text="8", padx=14, pady=10, font=("Arial",12,"bold"))
        bt8.grid(row=2,column=0)
        bt9 = Button(top, text="9", padx=14, pady=10, font=("Arial",12,"bold"))
        bt9.grid(row=2,column=1)
        bt10 = Button(top, text="10", padx=10, pady=10, font=("Arial",12,"bold"))
        bt10.grid(row=2,column=2)
        bt11 = Button(top, text="11", padx=10, pady=10, font=("Arial",12,"bold"))
        bt11.grid(row=2,column=3)
        bt12 = Button(top, text="12", padx=10, pady=10, font=("Arial",12,"bold"))
        bt12.grid(row=2,column=4)
        bt13 = Button(top, text="13", padx=10, pady=10, font=("Arial",12,"bold"))
        bt13.grid(row=2,column=5)
        bt14 = Button(top, text="14", padx=10, pady=10, font=("Arial",12,"bold"))
        bt14.grid(row=2,column=6)
        bt15 = Button(top, text="15", padx=10, pady=10, font=("Arial",12,"bold"))
        bt15.grid(row=3,column=0)
        bt16 = Button(top, text="16", padx=10, pady=10, font=("Arial",12,"bold"))
        bt16.grid(row=3,column=1)
        bt17 = Button(top, text="17", padx=10, pady=10, font=("Arial",12,"bold"))
        bt17.grid(row=3,column=2)
        bt18 = Button(top, text="18", padx=10, pady=10, font=("Arial",12,"bold"))
        bt18.grid(row=3,column=3)
        bt19 = Button(top, text="19", padx=10, pady=10, font=("Arial",12,"bold"))
        bt19.grid(row=3,column=4)
        bt20 = Button(top, text="20", padx=10, pady=10, font=("Arial",12,"bold"))
        bt20.grid(row=3,column=5)
        bt21 = Button(top, text="21", padx=10, pady=10, font=("Arial",12,"bold"))
        bt21.grid(row=3,column=6)
        bt22 = Button(top, text="22", padx=10, pady=10, font=("Arial",12,"bold"))
        bt22.grid(row=4,column=0)
        bt23 = Button(top, text="23", padx=10, pady=10, font=("Arial",12,"bold"))
        bt23.grid(row=4,column=1)
        bt24 = Button(top, text="24", padx=10, pady=10, font=("Arial",12,"bold"))
        bt24.grid(row=4,column=2)
        bt25 = Button(top, text="25", padx=10, pady=10, font=("Arial",12,"bold"))
        bt25.grid(row=4,column=3)
        bt26 = Button(top, text="26", padx=10, pady=10, font=("Arial",12,"bold"))
        bt26.grid(row=4,column=4)
        bt27 = Button(top, text="27", padx=10, pady=10, font=("Arial",12,"bold"))
        bt27.grid(row=4,column=5)
        bt28 = Button(top, text="28", padx=10, pady=10, font=("Arial",12,"bold"))
        bt28.grid(row=4,column=6)
        bt29 = Button(top, text="29", padx=10, pady=10, font=("Arial",12,"bold"))
        bt29.grid(row=5,column=0)
        bt30 = Button(top, text="30", padx=10, pady=10, font=("Arial",12,"bold"))
        bt30.grid(row=5,column=1)
        bt31 = Button(top, text="31", padx=10, pady=10, font=("Arial",12,"bold"))
        bt31.grid(row=5,column=2)   
    elif p_bulan == "Februari":
        bt1 = Button(top, text="1", padx=14, pady=10, font=("Arial",12,"bold"))
        bt1.grid(row=1,column=0)
        bt2 = Button(top, text="2", padx=14, pady=10, font=("Arial",12,"bold"))
        bt2.grid(row=1,column=1)
        bt3 = Button(top, text="3", padx=14, pady=10, font=("Arial",12,"bold"))
        bt3.grid(row=1,column=2)
        bt4 = Button(top, text="4", padx=14, pady=10, font=("Arial",12,"bold"))
        bt4.grid(row=1,column=3)
        bt5 = Button(top, text="5", padx=14, pady=10, font=("Arial",12,"bold"))
        bt5.grid(row=1,column=4)
        bt6 = Button(top, text="6", padx=14, pady=10, font=("Arial",12,"bold"))
        bt6.grid(row=1,column=5)
        bt7 = Button(top, text="7", padx=14, pady=10, font=("Arial",12,"bold"))
        bt7.grid(row=1,column=6)
        bt8 = Button(top, text="8", padx=14, pady=10, font=("Arial",12,"bold"))
        bt8.grid(row=2,column=0)
        bt9 = Button(top, text="9", padx=14, pady=10, font=("Arial",12,"bold"))
        bt9.grid(row=2,column=1)
        bt10 = Button(top, text="10", padx=10, pady=10, font=("Arial",12,"bold"))
        bt10.grid(row=2,column=2)
        bt11 = Button(top, text="11", padx=10, pady=10, font=("Arial",12,"bold"))
        bt11.grid(row=2,column=3)
        bt12 = Button(top, text="12", padx=10, pady=10, font=("Arial",12,"bold"))
        bt12.grid(row=2,column=4)
        bt13 = Button(top, text="13", padx=10, pady=10, font=("Arial",12,"bold"))
        bt13.grid(row=2,column=5)
        bt14 = Button(top, text="14", padx=10, pady=10, font=("Arial",12,"bold"))
        bt14.grid(row=2,column=6)
        bt15 = Button(top, text="15", padx=10, pady=10, font=("Arial",12,"bold"))
        bt15.grid(row=3,column=0)
        bt16 = Button(top, text="16", padx=10, pady=10, font=("Arial",12,"bold"))
        bt16.grid(row=3,column=1)
        bt17 = Button(top, text="17", padx=10, pady=10, font=("Arial",12,"bold"))
        bt17.grid(row=3,column=2)
        bt18 = Button(top, text="18", padx=10, pady=10, font=("Arial",12,"bold"))
        bt18.grid(row=3,column=3)
        bt19 = Button(top, text="19", padx=10, pady=10, font=("Arial",12,"bold"))
        bt19.grid(row=3,column=4)
        bt20 = Button(top, text="20", padx=10, pady=10, font=("Arial",12,"bold"))
        bt20.grid(row=3,column=5)
        bt21 = Button(top, text="21", padx=10, pady=10, font=("Arial",12,"bold"))
        bt21.grid(row=3,column=6)
        bt22 = Button(top, text="22", padx=10, pady=10, font=("Arial",12,"bold"))
        bt22.grid(row=4,column=0)
        bt23 = Button(top, text="23", padx=10, pady=10, font=("Arial",12,"bold"))
        bt23.grid(row=4,column=1)
        bt24 = Button(top, text="24", padx=10, pady=10, font=("Arial",12,"bold"))
        bt24.grid(row=4,column=2)
        bt25 = Button(top, text="25", padx=10, pady=10, font=("Arial",12,"bold"))
        bt25.grid(row=4,column=3)
        bt26 = Button(top, text="26", padx=10, pady=10, font=("Arial",12,"bold"))
        bt26.grid(row=4,column=4)
        bt27 = Button(top, text="27", padx=10, pady=10, font=("Arial",12,"bold"))
        bt27.grid(row=4,column=5)
        bt28 = Button(top, text="28", padx=10, pady=10, font=("Arial",12,"bold"))
        bt28.grid(row=4,column=6)
    else :
        bt31 = Button(top, text="31", padx=10, pady=10, font=("Arial",12,"bold"))
        bt31.grid(row=5,column=2)

why the if statement dont work, and just the else statement will work? i want to make the button in the new window according to month which will be selected in the combobox option. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

CodePudding user response:

  • Comment out line 8 and 10.
  • Add parameter in buka() function.
  • Line 14, Use slice instead of if p_bulan == "Januari":
  • Add b_bulan.bind('<<ComboboxSelected>>', buka) next to mainloop()

Here is code:

from tkinter import *
from tkinter import ttk
root = Tk()

b = StringVar()
nb =["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"]
b_bulan = ttk.Combobox(root, textvariable=b, values=nb)
#b_bulan.bind("<<ComboboxSelected>>")
b_bulan.pack()
#p_bulan= "<<ComboboxSelected>>"

def buka(event):
    top = Toplevel()
    top.title("Waktu Sholat")
    head = Label(top, text="Bulan", font=("Arial",25,"bold"), padx=184, pady=10)
    head.grid(row=0,column=0,columnspan=7)
    if nb[0]:
        bt1 = Button(top, text="1", padx=4, pady=5, font=("Arial",12,"bold"))
        bt1.grid(row=1,column=0)
        bt2 = Button(top, text="2", padx=14, pady=10, font=("Arial",12,"bold"))
        bt2.grid(row=1,column=1)
        bt3 = Button(top, text="3", padx=14, pady=10, font=("Arial",12,"bold"))
        bt3.grid(row=1,column=2)
        bt4 = Button(top, text="4", padx=14, pady=10, font=("Arial",12,"bold"))
        bt4.grid(row=1,column=3)
        bt5 = Button(top, text="5", padx=14, pady=10, font=("Arial",12,"bold"))
        bt5.grid(row=1,column=4)
        bt6 = Button(top, text="6", padx=14, pady=10, font=("Arial",12,"bold"))
        bt6.grid(row=1,column=5)
        bt7 = Button(top, text="7", padx=14, pady=10, font=("Arial",12,"bold"))
        bt7.grid(row=1,column=6)
        bt8 = Button(top, text="8", padx=14, pady=10, font=("Arial",12,"bold"))
        bt8.grid(row=2,column=0)
        bt9 = Button(top, text="9", padx=14, pady=10, font=("Arial",12,"bold"))
        bt9.grid(row=2,column=1)
        bt10 = Button(top, text="10", padx=10, pady=10, font=("Arial",12,"bold"))
        bt10.grid(row=2,column=2)
        bt11 = Button(top, text="11", padx=10, pady=10, font=("Arial",12,"bold"))
        bt11.grid(row=2,column=3)
        bt12 = Button(top, text="12", padx=10, pady=10, font=("Arial",12,"bold"))
        bt12.grid(row=2,column=4)
        bt13 = Button(top, text="13", padx=10, pady=10, font=("Arial",12,"bold"))
        bt13.grid(row=2,column=5)
        bt14 = Button(top, text="14", padx=10, pady=10, font=("Arial",12,"bold"))
        bt14.grid(row=2,column=6)
        bt15 = Button(top, text="15", padx=10, pady=10, font=("Arial",12,"bold"))
        bt15.grid(row=3,column=0)
        bt16 = Button(top, text="16", padx=10, pady=10, font=("Arial",12,"bold"))
        bt16.grid(row=3,column=1)
        bt17 = Button(top, text="17", padx=10, pady=10, font=("Arial",12,"bold"))
        bt17.grid(row=3,column=2)
        bt18 = Button(top, text="18", padx=10, pady=10, font=("Arial",12,"bold"))
        bt18.grid(row=3,column=3)
        bt19 = Button(top, text="19", padx=10, pady=10, font=("Arial",12,"bold"))
        bt19.grid(row=3,column=4)
        bt20 = Button(top, text="20", padx=10, pady=10, font=("Arial",12,"bold"))
        bt20.grid(row=3,column=5)
        bt21 = Button(top, text="21", padx=10, pady=10, font=("Arial",12,"bold"))
        bt21.grid(row=3,column=6)
        bt22 = Button(top, text="22", padx=10, pady=10, font=("Arial",12,"bold"))
        bt22.grid(row=4,column=0)
        bt23 = Button(top, text="23", padx=10, pady=10, font=("Arial",12,"bold"))
        bt23.grid(row=4,column=1)
        bt24 = Button(top, text="24", padx=10, pady=10, font=("Arial",12,"bold"))
        bt24.grid(row=4,column=2)
        bt25 = Button(top, text="25", padx=10, pady=10, font=("Arial",12,"bold"))
        bt25.grid(row=4,column=3)
        bt26 = Button(top, text="26", padx=10, pady=10, font=("Arial",12,"bold"))
        bt26.grid(row=4,column=4)
        bt27 = Button(top, text="27", padx=10, pady=10, font=("Arial",12,"bold"))
        bt27.grid(row=4,column=5)
        bt28 = Button(top, text="28", padx=10, pady=10, font=("Arial",12,"bold"))
        bt28.grid(row=4,column=6)
        bt29 = Button(top, text="29", padx=10, pady=10, font=("Arial",12,"bold"))
        bt29.grid(row=5,column=0)
        bt30 = Button(top, text="30", padx=10, pady=10, font=("Arial",12,"bold"))
        bt30.grid(row=5,column=1)
        bt31 = Button(top, text="31", padx=10, pady=10, font=("Arial",12,"bold"))
        bt31.grid(row=5,column=2)   
    elif nb[1]:
        bt1 = Button(top, text="1", padx=14, pady=10, font=("Arial",12,"bold"))
        bt1.grid(row=1,column=0)
        bt2 = Button(top, text="2", padx=14, pady=10, font=("Arial",12,"bold"))
        bt2.grid(row=1,column=1)
        bt3 = Button(top, text="3", padx=14, pady=10, font=("Arial",12,"bold"))
        bt3.grid(row=1,column=2)
        bt4 = Button(top, text="4", padx=14, pady=10, font=("Arial",12,"bold"))
        bt4.grid(row=1,column=3)
        bt5 = Button(top, text="5", padx=14, pady=10, font=("Arial",12,"bold"))
        bt5.grid(row=1,column=4)
        bt6 = Button(top, text="6", padx=14, pady=10, font=("Arial",12,"bold"))
        bt6.grid(row=1,column=5)
        bt7 = Button(top, text="7", padx=14, pady=10, font=("Arial",12,"bold"))
        bt7.grid(row=1,column=6)
        bt8 = Button(top, text="8", padx=14, pady=10, font=("Arial",12,"bold"))
        bt8.grid(row=2,column=0)
        bt9 = Button(top, text="9", padx=14, pady=10, font=("Arial",12,"bold"))
        bt9.grid(row=2,column=1)
        bt10 = Button(top, text="10", padx=10, pady=10, font=("Arial",12,"bold"))
        bt10.grid(row=2,column=2)
        bt11 = Button(top, text="11", padx=10, pady=10, font=("Arial",12,"bold"))
        bt11.grid(row=2,column=3)
        bt12 = Button(top, text="12", padx=10, pady=10, font=("Arial",12,"bold"))
        bt12.grid(row=2,column=4)
        bt13 = Button(top, text="13", padx=10, pady=10, font=("Arial",12,"bold"))
        bt13.grid(row=2,column=5)
        bt14 = Button(top, text="14", padx=10, pady=10, font=("Arial",12,"bold"))
        bt14.grid(row=2,column=6)
        bt15 = Button(top, text="15", padx=10, pady=10, font=("Arial",12,"bold"))
        bt15.grid(row=3,column=0)
        bt16 = Button(top, text="16", padx=10, pady=10, font=("Arial",12,"bold"))
        bt16.grid(row=3,column=1)
        bt17 = Button(top, text="17", padx=10, pady=10, font=("Arial",12,"bold"))
        bt17.grid(row=3,column=2)
        bt18 = Button(top, text="18", padx=10, pady=10, font=("Arial",12,"bold"))
        bt18.grid(row=3,column=3)
        bt19 = Button(top, text="19", padx=10, pady=10, font=("Arial",12,"bold"))
        bt19.grid(row=3,column=4)
        bt20 = Button(top, text="20", padx=10, pady=10, font=("Arial",12,"bold"))
        bt20.grid(row=3,column=5)
        bt21 = Button(top, text="21", padx=10, pady=10, font=("Arial",12,"bold"))
        bt21.grid(row=3,column=6)
        bt22 = Button(top, text="22", padx=10, pady=10, font=("Arial",12,"bold"))
        bt22.grid(row=4,column=0)
        bt23 = Button(top, text="23", padx=10, pady=10, font=("Arial",12,"bold"))
        bt23.grid(row=4,column=1)
        bt24 = Button(top, text="24", padx=10, pady=10, font=("Arial",12,"bold"))
        bt24.grid(row=4,column=2)
        bt25 = Button(top, text="25", padx=10, pady=10, font=("Arial",12,"bold"))
        bt25.grid(row=4,column=3)
        bt26 = Button(top, text="26", padx=10, pady=10, font=("Arial",12,"bold"))
        bt26.grid(row=4,column=4)
        bt27 = Button(top, text="27", padx=10, pady=10, font=("Arial",12,"bold"))
        bt27.grid(row=4,column=5)
        bt28 = Button(top, text="28", padx=10, pady=10, font=("Arial",12,"bold"))
        bt28.grid(row=4,column=6)
    else :
        bt31 = Button(top, text="31", padx=10, pady=10, font=("Arial",12,"bold"))
        bt31.grid(row=5,column=2)


b_bulan.bind('<<ComboboxSelected>>', buka)
root.mainloop()        

Screenshot : The first top image is January and the bottom is February.

enter image description here

  • Related