为tkinter中的每个选项菜单添加标签

时间:2017-07-31 12:26:37

标签: python tkinter

我有一个小部件,其中有许多不同的选项菜单。我需要在每个选项菜单中添加左侧的相应标签。

我的代码如下所示:

.invert {
    filter: grayscale(1) invert(100%);
    -webkit-filter: grayscale(1) invert(100%);
}

<body class="invert"> <!-- Toggle invert class using javascript -->

结果如下:

The result looks like this

但我需要每个标签都在适当的行,并带有下拉列表

在Excel中它看起来像这样:

enter image description here

B列中的每一行都是下拉列表。

我知道from tkinter import* class MyOptionMenu(OptionMenu): def __init__(self, master, status, *options): self.var = StringVar(master) self.var.set(status) OptionMenu.__init__(self, master, self.var, *options) self.config(font=('calibri',(8)),bg='white',width=20) self['menu'].config(font=('calibri',(8)),bg='white') root = Tk() optionList1 = items1 optionList2 = items2 optionList3 = items3 optionList4 = items4 optionList5 = items5 lab1 = Label(root, text="condition №1", font="Arial 8", anchor='w') mymenu1 = MyOptionMenu(root, '-', *optionList1) lab2 = Label(root, text="condition №2", font="Arial 8", anchor='w') mymenu2 = MyOptionMenu(root, '-', *optionList2) lab3 = Label(root, text="condition №3", font="Arial 8", anchor='w') mymenu3 = MyOptionMenu(root, '-', *optionList3) lab4 = Label(root, text="condition №4", font="Arial 8", anchor='w') mymenu4 = MyOptionMenu(root, '-', *optionList4) lab = Label(root, text="Enter the date", font="Arial 8", anchor='w') ent1 = Entry(root,width=20,bd=3) lab5 = Label(root, text="condition №5", font="Arial 8", anchor='w') mymenu5 = MyOptionMenu(root, '-', *optionList5) lab1.pack(side="top",fill = "x") mymenu1.pack(side="top",fill = "y") lab2.pack(side="top",fill = "x") mymenu2.pack(side="top",fill = "y") lab3.pack(side="top", fill="x") mymenu3.pack(side="top", fill="y") lab4.pack(side="top", fill="x") mymenu4.pack(side="top", fill = "y") lab.pack(side="top", fill="x") ent1.pack(side="top", fill="y") lab5.pack(side="top", fill="x") mymenu5.pack(side="top", fill = "y") def save_selected_values(): global values1 values1 = [mymenu1.var.get(), mymenu2.var.get(), mymenu3.var.get(), mymenu4.var.get(), ent1.get(), mymenu5.var.get()] print(values1) button = Button(root, text="OK", command=save_selected_values) button.pack() root.mainloop() 填满整行,但当我尝试更改它时,它看起来更糟。

我很感激任何建议!

1 个答案:

答案 0 :(得分:2)

作为给定的Excel示例,我会将grid geometry manager用于您的目的,这会将项目放在网格布局中。有了它,您可以指定行和列。我还会将所有标签和下拉列表存储在一个列表中,以便于访问。然后你可以使用:

for index, (lab, mymenu) in enumerate(zip(labels, mymenus)):
    lab.grid(row=index, column=0)
    mymenu.grid(row=index, column=1)