Tkinter - 为列表框设置标签

时间:2016-04-05 15:30:15

标签: python tkinter label

我正在尝试在Tkinter中执行简单的 GUI 。我有一个Listbox,我希望用户知道Listbox中的数据类型,所以我想为它设置一个标签(上面)。

问题在于,当我设置标签时,此Listbox会消失。

l = Label(multiple_choose_days_listbox, textvariable=label_day_listbox , anchor=NW, justify='center')
l.pack()

解决方案可能很明显,但我是Tkinter中的新手。

你有什么建议吗?

import Tkinter
import tkSimpleDialog
from Tkinter import *

import db
import ttkcalendar


class CalendarDialog(tkSimpleDialog.Dialog):
    """Dialog box that displays a calendar and returns the selected date"""

    def __init__(self, master):
        self.calendar = ttkcalendar.Calendar(master)
        # self.calendar.pack()

    @property
    def result(self):
        return self.calendar.selection

    def pack(self,**kwargs):
        self.calendar.pack(**kwargs)


states_list = db.get_states()
bought_days_before_list = db.get_bought_days_before()
multiple_choose_length_of_trips_list = db.get_lengths_of_trips()


def main():
    root = Tkinter.Tk()
    root.wm_title("CalendarDialog Demo")
    root.grid_columnconfigure(0, weight=1)
    root.grid_rowconfigure(0, weight=1)
    root.geometry("1000x500")
    top = Frame(root)
    bottom = Frame(root)
    top.pack(side=TOP)
    bottom.pack(side=BOTTOM, fill=BOTH, expand=True)

    cd_1 = CalendarDialog(root)
    cd_2 = CalendarDialog(root)
    cd_1.pack(in_=top, side=LEFT)
    cd_2.pack(in_=top, side=LEFT)



    multiple_choose_states_listbox = Listbox(root, selectmode=EXTENDED)
    multiple_choose_states_listbox.grid(row=20, columns=1)
    multiple_choose_days_listbox = Listbox(root, selectmode=EXTENDED)
    multiple_choose_length_of_trips_list_listbox = Listbox(root, selectmode=EXTENDED)
    label_day_listbox = StringVar()
    label_day_listbox.set("LABEL")
    l = Label(multiple_choose_days_listbox, textvariable=label_day_listbox , anchor=NW, justify='center')
    l.pack()

    multiple_choose_days_listbox.pack(in_=top,side=LEFT)
    multiple_choose_states_listbox.pack()
    multiple_choose_length_of_trips_list_listbox.pack()

    for item in states_list:
        multiple_choose_states_listbox.insert(END, item)

    for item in bought_days_before_list:
        multiple_choose_days_listbox.insert(END, item)

    def get_result_dict():
        date_from = cd_1.result
        date_to = cd_2.result
        states = [states_list[i] for i in multiple_choose_states_listbox.curselection()]
        bought_days_before = [bought_days_before_list[i] for i in multiple_choose_days_listbox.curselection()]
        length_of_trip = [states_list[i] for i in multiple_choose_length_of_trips_list.curselection()]

        res_dict = {
            'date_from': date_from,
            'date_to': date_to,
            'states': states,
            'bought_days_before': bought_days_before,
            'length_of_trip': length_of_trip,
        }

        return res_dict

    button = Tkinter.Button(root, text="Generate", command=get_result_dict)
    button.pack()

    root.update()
    root.mainloop()


if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

应该是

l = Tkinter.Label(root, text="Label")