OptionMenu无法在子Tkinter窗口中工作

时间:2017-06-26 23:39:04

标签: python tkinter

我使用Tkinter创建了一个子窗口。子窗口中有下拉菜单。当我运行子窗口(运行subMenu_compareBOM.py)时,它工作正常。但是,如果我从父窗口运行子窗口,则不会出现下拉菜单选项。是否有任何理由为什么OptionMenu在子窗口而不是独立窗口中的工作方式不同?

以下是窗口的代码和图片以供参考。提前谢谢!

Dropdown menu selection does not show in child window

from common import *
from tkinter import * # imports tkinter, a package of functions for creating user interfaces.
from tkinter import messagebox, filedialog,ttk # Unsure why this is needed (line directly above should also do this) but it works.
from compareBOM import *

def subMenu_compareBOM():

# Type Function Title and Function Description here.  Propagates throughout window.
functionTitle = 'Compare BOMs'
functionDescription = 'Compares two different BOMs or files.\nOutputs a list comparing the two BOMs.' 
fileAInstructions = '1) Load first BOM and select the file type (e.g. CF export, SOE BOM, etc.)'
fileBInstructions = '2) Load second BOM and select the file type (e.g. CF export, SOE BOM, etc.)'

# Open new window
root = Tk()
root.wm_title(functionTitle) # Title of window/function goes here

# Creates title text box
title = Text(root,height=1,width=30)
title.tag_configure("center",justify="center")

# Adds text to title
title.insert(INSERT,functionTitle,"center")
title.insert(INSERT,'',"center")

# Places title text box
title.grid(column=0,row=0,columnspan = 5,sticky="news")

# Creates text box that describes function.
description = Text(root,height=6,width=30)

# Adds text to description
description.insert(INSERT,functionDescription,"center")
description.insert(INSERT,'',"center")
# Places description below title
description.grid(column=0,row=1,columnspan = 5,sticky="news")

# Creates instructions to load the files that are loaded into the system
descriptionFileA = Text(root,width=40,height=2)
descriptionFileA.insert(INSERT,fileAInstructions)
descriptionFileA.grid(column=2,row=2,columnspan = 3,sticky="news")

descriptionFileB = Text(root,width=40,height=2)
descriptionFileB.insert(INSERT,fileBInstructions)
descriptionFileB.grid(column=2,row=4,columnspan = 3,sticky="news")

descriptionRunButton = Text(root,width=40,height=1)
descriptionRunButton.insert(INSERT,'3) Click Run Function')
descriptionRunButton.grid(column=2,row=6,columnspan = 3,sticky="news")

# Creates variables to import file A.
global fileNameA # This is the name of the first file ('File A') loaded into the system
global displayFileNameA # Defines the Lattice Import button as a global variable (can be called outside this function)

# Creates button to import file A.
buttonImportFileA = Button(root,text = 'Load .csv File',width=20,height=1,command = importFileA) # generates button for loading files into the system
buttonImportFileA.grid(column=2,row=3,sticky="news") 

# Creates text box to display imported file A's name.
displayFileNameA = Text(root,height=1,width = 40)
displayFileNameA.insert(INSERT,'')
displayFileNameA.grid(column=3,row=3,sticky="news") 

# Creates variables to import file B.
global fileNameB # This is the name of the first file ('File A') loaded into the system
global displayFileNameB # Defines the Lattice Import button as a global variable (can be called outside this function)

# Creates button to import file B.
buttonImportFileB = Button(root,text = 'Load .csv File',width=20,height=1,command = importFileB) # generates button for loading files into the system
buttonImportFileB.grid(column=2,row=5,sticky="news") 

# Creates text box to display imported file B's name.
displayFileNameB = Text(root,height=1,width = 40)
displayFileNameB.insert(INSERT,'')
displayFileNameB.grid(column=3,row=5,sticky="news") 

# Creates drop-down menus to select imported file types.

fileTypeA = StringVar() # This is the file type for File A
fileTypeA.set('Select file type')
fileTypeMenuA = OptionMenu(root,fileTypeA,'CellFusion Export','ZCAU Export','SOE BOM','Lattice Assembly List','Lattice Process List')
fileTypeMenuA.grid(column=4,row=3,sticky="news") 

fileTypeB = StringVar()
fileTypeB.set('Select file type')
fileTypeMenuB = OptionMenu(root,fileTypeB,'CellFusion Export','ZCAU Export','SOE BOM','Lattice Assembly List','Lattice Process List')
fileTypeMenuB.grid(column=4,row=5,sticky="news") 

# creates button to run function
buttonRunFunction = Button(root,text = 'Run Function',width = 20,height = 1,command=lambda:compareBOM(fileNameA,fileTypeA.get(),fileNameB,fileTypeB.get()))
buttonRunFunction.grid(column=4,row=7,sticky='news')

mainloop()

# Function that loads files into our program
def importFileA():
    global fileNameA
    fileNameA =  filedialog.askopenfilename() # Opens file dialog to select file
    displayFileNameA.delete("1.0",END)
    displayFileNameA.insert(INSERT,fileNameA) # adds new file name in the display

def importFileB():
    global fileNameB
    fileNameB =  filedialog.askopenfilename() # Opens file dialog to select file
    displayFileNameB.delete("1.0",END) # Deletes text previously in file name display
    displayFileNameB.insert(INSERT,fileNameB) # adds new file name in the display

0 个答案:

没有答案