如何使用输入和按钮替换标签

时间:2018-12-05 14:49:17

标签: python-3.x tkinter

所以我试图使用搜索框显示标签,但是每次我单击搜索按钮时,新标签都会显示在前一个标签的正下方,因此我希望标签只是更改而不是出现在前一个标签下

Label(myWindow, text=" ").pack()
def box():
    input_ = entry_1.get()
    display = "JADWAL HARI " + input_.upper()
    label_display = Label(myWindow, font="none 16", relief="sunken")
    label_display["text"] = display
    label_display.pack()
    if (input_.upper() == "SENIN"):
        Label(myWindow, text="{}\n{} | {} | {}\n{}".format(List.list_hari[0], List.list_mapel[0], List.list_waktu[0], List.list_kelas[1], List.list_dosen[0]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[1], List.list_waktu[1], List.list_kelas[0], List.list_dosen[1]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[2], List.list_waktu[2], List.list_kelas[9], List.list_dosen[2]), font="none 14").pack()
    elif(input_.upper() == "SELASA"):
        Label(myWindow, text="{}\n{} | {} | {}\n{}".format(List.list_hari[1], List.list_mapel[3], List.list_waktu[0], List.list_kelas[8], List.list_dosen[3]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[1], List.list_waktu[1], List.list_kelas[0], List.list_dosen[1]), font="none 14").pack()
    elif(input_.upper() == "RABU"):
        Label(myWindow, text="{}\n{} | {} | {}\n{}".format(List.list_hari[2], List.list_mapel[3], List.list_waktu[0], List.list_kelas[8], List.list_dosen[3]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[4], List.list_waktu[1], List.list_kelas[0], List.list_dosen[4]), font="none 14").pack()
    elif(input_.upper() == "KAMIS"):
        Label(myWindow, text="{}\n{} | {} | {}\n{}".format(List.list_hari[3], List.list_mapel[6], List.list_waktu[1], List.list_kelas[5], List.list_dosen[6]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[5], List.list_waktu[0], List.list_kelas[6], List.list_dosen[5]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[10], List.list_waktu[3], List.list_kelas[4], List.list_dosen[10]), font="none 14").pack()
    elif(input_.upper() == "JUMAT"):
        Label(myWindow, text="{}\n{} | {} | {}\n{}".format(List.list_hari[4], List.list_mapel[7], List.list_waktu[0], List.list_kelas[2], List.list_dosen[7]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[8], List.list_waktu[1], List.list_kelas[2], List.list_dosen[8]), font="none 14").pack()
        Label(myWindow, text="--------------------------------------------", font="none 14").pack()
        Label(myWindow, text="{} | {} | {}\n{}".format(List.list_mapel[9], List.list_waktu[3], List.list_kelas[0], List.list_dosen[9]), font="none 14").pack()

entry_1 = Entry(myWindow)
button_1 = Button(myWindow, text="cari jadwal", command=box)

entry_1.pack()
button_1.pack()

1 个答案:

答案 0 :(得分:0)

由于每次单击搜索按钮时,实际上都会创建新标签并附加到主窗口。为了更新现有标签,您需要如下更改代码:

message = Label(myWindow, text="")
message.pack()

def box():
    input_ = entry_1.get().upper()
    display = "JADWAL HARI " + input_
    if input == "SENIN":
        display += "\n......"
    elif input == "SELASA":
        display += "\n......"
    elif input == "RABU":
        display += "\n......"
    message['text'] = display     # update label
    entry_1.delete(0, END)        # clear entry