如何在tkinter Python中连接组合框选项和if语句?

vi4fp9gy  于 2022-12-20  发布在  Python
关注(0)|答案(1)|浏览(146)

我用tkinter做了一个图形用户界面我有这样的代码:

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)

为什么if语句不起作用,只有else语句起作用?我想根据月份在新窗口中创建按钮,月份将在组合框选项中选择。

5fjcxozz

5fjcxozz1#

  • 注解掉第8行和第10行。
  • buka()函数中添加参数。
  • 第14行,使用切片代替if p_bulan == "Januari":
  • mainloop()旁边添加b_bulan.bind('<<ComboboxSelected>>', buka)

代码如下:

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

截图:第一个顶部图像是一月,底部是二月。

相关问题