1

我正在尝试在我正在构建的图形界面上放置一些下拉列表。

我为下拉列表找到了以下代码,但我无法使其适应我的代码。

from Tkinter import *

def print_it(event):
    print var.get()

root = Tk()
var = StringVar()
var.set("a")
OptionMenu(root, var, "a","b","c", command=print_it).pack()
root.mainloop() 

这是我的代码,到目前为止我所做的非常简单。出现一个菜单,它询问用户想要输入多少 (n) 个组件,并显示 n 个输入选项。上面的代码在您放置所需数量的组件后显示“空白”条目。我想用三个下拉列表替换这三个空白条目。

当我想放置这些下拉列表时,它会被标记。

from Tkinter import *
import Image
import ImageTk
import tkFileDialog

class Planificador:
    def __init__(self,master):
        master.title("Planificador")
        self.frameOne = Frame(master)
        self.frameOne.grid(row=0,column=0)

        # Logo
        self.imgLabel = Label(self.frameOne, image = None)
        self.imgLabel.grid(row=0,column=0)
        self.img = ImageTk.PhotoImage(file = "logo.png")
        self.imgLabel["image"] = self.img

        self.botones()

    def botones(self):
        self.piezastext = Label(self.frameOne, text = " number of components ", justify="center")
        self.piezastext.grid(row=1, column=0)
        self.entrypiezas = Entry(self.frameOne,width=5)
        self.entrypiezas.grid(row=2, column=0)
        self.aceptarnumpiezas = Button(self.frameOne,text="Aceptar", command=self.aceptar_piezas,width=8)
        self.aceptarnumpiezas.grid(row=6, column=0)


    def aceptar_piezas(self):
        num_piezas = self.entrypiezas.get()
        print num_piezas

        self.piezastext.grid_remove()
        self.entrypiezas.grid_remove()
        self.aceptarnumpiezas.grid_remove()

        n = 1;
        while n <= int(num_piezas):
            self.textopieza = Label(self.frameOne, text = "Pieza", justify="left")
            self.textopieza.grid(row=n, column=0)

                    // INSTEAD THESE 'n' BLANK ENTRYS, I WANT TO PUT 'n' DROP DOWN LISTS
            self.entrypiezas = Entry(self.frameOne,width=5)
            self.entrypiezas.grid(row=n, column=1)

            self.aceptarpiezas = Button(self.frameOne,text="Aceptar",width=8)
            self.aceptarpiezas.grid(row=int(num_piezas)+1, column=0)

            n += 1


# Main
if __name__ == "__main__":
    # create interfacE
    root = Tk()
    movieApp = Planificador(root)
    root.mainloop()

所以我想知道如何将下拉列表放在给定的框架上,frameOne在我的情况下,而不是一个完整的窗口。提前致谢。

4

1 回答 1

2

我修改了你的aceptar_piezas函数来做我认为你想要的:

def aceptar_piezas(self):
    num_piezas = self.entrypiezas.get()
    print num_piezas

    self.piezastext.grid_remove()
    self.entrypiezas.grid_remove()
    self.aceptarnumpiezas.grid_remove()
    # Create a list of tuples to hold the dynamically created Optionmenus
    # The first item in the tuple is the menu, the second is its variable
    self.optionmenus = list()
    n = 1
    while n <= int(num_piezas):
        self.textopieza = Label(self.frameOne, text = "Pieza", justify="left")
        self.textopieza.grid(row=n, column=0)

        # Variable for the Optionmenu
        var = StringVar()
        # The menu
        menu = OptionMenu(self.frameOne, var, "a","b","c")
        menu.grid(row=n, column=1)
        # Set the variable to "a" as default
        var.set("a")
        # Add the menu to the list of Optionmenus
        self.optionmenus.append((menu, var))

        n += 1
    def clicked():
        """This function was made just to demonstrate.  It is hooked up to the button"""
        for optionmenu in self.optionmenus:
            print optionmenu[1].get()
        print self.optionmenus
    # This button doesn't need to be in the while loop
    self.aceptarpiezas = Button(self.frameOne, text="Aceptar", command=clicked, width=8)
    self.aceptarpiezas.grid(row=int(num_piezas)+1, column=0)

列表中的元组按照创建选项菜单的顺序排列。因此,第一个元组包含第一个 Optionmenu 的数据,第二个包含第二个 Optionmenu 的数据,依此类推。

于 2013-07-24T16:43:15.403 回答