2013-07-24 23 views
1

我打算在我正在構建的圖形界面上放置一些下拉列表。試圖把代碼放在下拉列表上使用Tkinter

我發現了下拉列表中的下列代碼,但我無法將其適用於我的代碼。

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上,而不是一個完整的窗口。提前致謝。

+0

你已經告訴我們你想做什麼,但是你沒有問過問題。看起來你只是想讓別人爲你做這件事,而這不是你想要的 - 嘗試一下,如果你被困住了,就回來一個問題。 –

+0

問題是我搞亂了'OptionMenu(root,var,「a」,「b」,「c」,command = print_it).pack()'''root'部分。我想把它放在我的框架上而不是'root'窗口中。 –

+0

這不是一個問題。你在說明你在做什麼。你還沒有告訴我們它是如何工作的,因爲你需要它,或者詢問一個具體的問題。 –

回答

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) 

在列表中的元組,該Optionmenus創建順序。因此,第一個元組包含第一個選項菜單的數據,第二個包含第二個,等等。